{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "import os\n",
    "import lightgbm as lgb\n",
    "import warnings\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from sklearn.model_selection import StratifiedKFold, KFold\n",
    "import gc\n",
    "from gensim.models import Word2Vec\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "train2018 = pd.read_csv('../管网压力预测-数据/train_水压数据_2018.csv')\n",
    "train2019 = pd.read_csv('../管网压力预测-数据/train_水压数据_2019.csv')\n",
    "train2020 = pd.read_csv('../管网压力预测-数据/test_水压数据_2020.csv')\n",
    "test = pd.read_csv('../管网压力预测-数据/to_predict.csv')\n",
    "submit = pd.read_csv('../管网压力预测-数据/submit.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Time</th>\n",
       "      <th>MeasName</th>\n",
       "      <th>H0</th>\n",
       "      <th>H1</th>\n",
       "      <th>H2</th>\n",
       "      <th>H3</th>\n",
       "      <th>H4</th>\n",
       "      <th>H5</th>\n",
       "      <th>H6</th>\n",
       "      <th>H7</th>\n",
       "      <th>...</th>\n",
       "      <th>H14</th>\n",
       "      <th>H15</th>\n",
       "      <th>H16</th>\n",
       "      <th>H17</th>\n",
       "      <th>H18</th>\n",
       "      <th>H19</th>\n",
       "      <th>H20</th>\n",
       "      <th>H21</th>\n",
       "      <th>H22</th>\n",
       "      <th>H23</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>站点4</td>\n",
       "      <td>0.402750</td>\n",
       "      <td>0.407625</td>\n",
       "      <td>0.418125</td>\n",
       "      <td>0.425250</td>\n",
       "      <td>0.426000</td>\n",
       "      <td>0.425250</td>\n",
       "      <td>0.417375</td>\n",
       "      <td>0.426375</td>\n",
       "      <td>...</td>\n",
       "      <td>0.348750</td>\n",
       "      <td>0.359250</td>\n",
       "      <td>0.355500</td>\n",
       "      <td>0.344250</td>\n",
       "      <td>0.352125</td>\n",
       "      <td>0.356250</td>\n",
       "      <td>0.347250</td>\n",
       "      <td>0.343875</td>\n",
       "      <td>0.356625</td>\n",
       "      <td>0.418875</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>站点7</td>\n",
       "      <td>0.214375</td>\n",
       "      <td>0.226750</td>\n",
       "      <td>0.232375</td>\n",
       "      <td>0.233125</td>\n",
       "      <td>0.235000</td>\n",
       "      <td>0.232750</td>\n",
       "      <td>0.230875</td>\n",
       "      <td>0.220000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.187375</td>\n",
       "      <td>0.196750</td>\n",
       "      <td>0.199750</td>\n",
       "      <td>0.192250</td>\n",
       "      <td>0.186250</td>\n",
       "      <td>0.183250</td>\n",
       "      <td>0.177250</td>\n",
       "      <td>0.163375</td>\n",
       "      <td>0.165250</td>\n",
       "      <td>0.199375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>站点22</td>\n",
       "      <td>0.247000</td>\n",
       "      <td>0.248125</td>\n",
       "      <td>0.271375</td>\n",
       "      <td>0.251125</td>\n",
       "      <td>0.272125</td>\n",
       "      <td>0.256375</td>\n",
       "      <td>0.257125</td>\n",
       "      <td>0.242500</td>\n",
       "      <td>...</td>\n",
       "      <td>0.245500</td>\n",
       "      <td>0.242875</td>\n",
       "      <td>0.238375</td>\n",
       "      <td>0.230875</td>\n",
       "      <td>0.237250</td>\n",
       "      <td>0.236875</td>\n",
       "      <td>0.236500</td>\n",
       "      <td>0.236500</td>\n",
       "      <td>0.241000</td>\n",
       "      <td>0.254500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>站点21</td>\n",
       "      <td>0.284250</td>\n",
       "      <td>0.289875</td>\n",
       "      <td>0.283500</td>\n",
       "      <td>0.281250</td>\n",
       "      <td>0.288375</td>\n",
       "      <td>0.288750</td>\n",
       "      <td>0.285750</td>\n",
       "      <td>0.255750</td>\n",
       "      <td>...</td>\n",
       "      <td>0.227625</td>\n",
       "      <td>0.238125</td>\n",
       "      <td>0.238500</td>\n",
       "      <td>0.218625</td>\n",
       "      <td>0.207000</td>\n",
       "      <td>0.212625</td>\n",
       "      <td>0.209250</td>\n",
       "      <td>0.189000</td>\n",
       "      <td>0.217875</td>\n",
       "      <td>0.270000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>站点20</td>\n",
       "      <td>0.292875</td>\n",
       "      <td>0.295875</td>\n",
       "      <td>0.305250</td>\n",
       "      <td>0.298875</td>\n",
       "      <td>0.310125</td>\n",
       "      <td>0.300750</td>\n",
       "      <td>0.288375</td>\n",
       "      <td>0.262500</td>\n",
       "      <td>...</td>\n",
       "      <td>0.247500</td>\n",
       "      <td>0.241125</td>\n",
       "      <td>0.243375</td>\n",
       "      <td>0.232500</td>\n",
       "      <td>0.233625</td>\n",
       "      <td>0.224250</td>\n",
       "      <td>0.219375</td>\n",
       "      <td>0.202125</td>\n",
       "      <td>0.219375</td>\n",
       "      <td>0.286500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         Time MeasName        H0        H1        H2        H3        H4  \\\n",
       "0  2018-01-01      站点4  0.402750  0.407625  0.418125  0.425250  0.426000   \n",
       "1  2018-01-01      站点7  0.214375  0.226750  0.232375  0.233125  0.235000   \n",
       "2  2018-01-01     站点22  0.247000  0.248125  0.271375  0.251125  0.272125   \n",
       "3  2018-01-01     站点21  0.284250  0.289875  0.283500  0.281250  0.288375   \n",
       "4  2018-01-01     站点20  0.292875  0.295875  0.305250  0.298875  0.310125   \n",
       "\n",
       "         H5        H6        H7  ...       H14       H15       H16       H17  \\\n",
       "0  0.425250  0.417375  0.426375  ...  0.348750  0.359250  0.355500  0.344250   \n",
       "1  0.232750  0.230875  0.220000  ...  0.187375  0.196750  0.199750  0.192250   \n",
       "2  0.256375  0.257125  0.242500  ...  0.245500  0.242875  0.238375  0.230875   \n",
       "3  0.288750  0.285750  0.255750  ...  0.227625  0.238125  0.238500  0.218625   \n",
       "4  0.300750  0.288375  0.262500  ...  0.247500  0.241125  0.243375  0.232500   \n",
       "\n",
       "        H18       H19       H20       H21       H22       H23  \n",
       "0  0.352125  0.356250  0.347250  0.343875  0.356625  0.418875  \n",
       "1  0.186250  0.183250  0.177250  0.163375  0.165250  0.199375  \n",
       "2  0.237250  0.236875  0.236500  0.236500  0.241000  0.254500  \n",
       "3  0.207000  0.212625  0.209250  0.189000  0.217875  0.270000  \n",
       "4  0.233625  0.224250  0.219375  0.202125  0.219375  0.286500  \n",
       "\n",
       "[5 rows x 26 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train2018.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Time</th>\n",
       "      <th>MeasName</th>\n",
       "      <th>H0</th>\n",
       "      <th>H1</th>\n",
       "      <th>H2</th>\n",
       "      <th>H3</th>\n",
       "      <th>H4</th>\n",
       "      <th>H5</th>\n",
       "      <th>H6</th>\n",
       "      <th>H7</th>\n",
       "      <th>...</th>\n",
       "      <th>H14</th>\n",
       "      <th>H15</th>\n",
       "      <th>H16</th>\n",
       "      <th>H17</th>\n",
       "      <th>H18</th>\n",
       "      <th>H19</th>\n",
       "      <th>H20</th>\n",
       "      <th>H21</th>\n",
       "      <th>H22</th>\n",
       "      <th>H23</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>站点4</td>\n",
       "      <td>0.417375</td>\n",
       "      <td>0.431625</td>\n",
       "      <td>0.437625</td>\n",
       "      <td>0.439500</td>\n",
       "      <td>0.447375</td>\n",
       "      <td>0.445125</td>\n",
       "      <td>0.436500</td>\n",
       "      <td>0.422250</td>\n",
       "      <td>...</td>\n",
       "      <td>0.337875</td>\n",
       "      <td>0.336000</td>\n",
       "      <td>0.339750</td>\n",
       "      <td>0.327000</td>\n",
       "      <td>0.320250</td>\n",
       "      <td>0.330750</td>\n",
       "      <td>0.336375</td>\n",
       "      <td>0.326625</td>\n",
       "      <td>0.350625</td>\n",
       "      <td>0.386625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>站点7</td>\n",
       "      <td>0.292375</td>\n",
       "      <td>0.321250</td>\n",
       "      <td>0.327250</td>\n",
       "      <td>0.324625</td>\n",
       "      <td>0.331750</td>\n",
       "      <td>0.316750</td>\n",
       "      <td>0.309250</td>\n",
       "      <td>0.298750</td>\n",
       "      <td>...</td>\n",
       "      <td>0.268000</td>\n",
       "      <td>0.267250</td>\n",
       "      <td>0.264625</td>\n",
       "      <td>0.261625</td>\n",
       "      <td>0.250375</td>\n",
       "      <td>0.256375</td>\n",
       "      <td>0.255625</td>\n",
       "      <td>0.237250</td>\n",
       "      <td>0.257875</td>\n",
       "      <td>0.291625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>站点22</td>\n",
       "      <td>0.255250</td>\n",
       "      <td>0.307000</td>\n",
       "      <td>0.327625</td>\n",
       "      <td>0.323500</td>\n",
       "      <td>0.343000</td>\n",
       "      <td>0.314500</td>\n",
       "      <td>0.304000</td>\n",
       "      <td>0.259375</td>\n",
       "      <td>...</td>\n",
       "      <td>0.231625</td>\n",
       "      <td>0.226375</td>\n",
       "      <td>0.228250</td>\n",
       "      <td>0.229000</td>\n",
       "      <td>0.230500</td>\n",
       "      <td>0.226375</td>\n",
       "      <td>0.229375</td>\n",
       "      <td>0.226000</td>\n",
       "      <td>0.229750</td>\n",
       "      <td>0.239500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>站点21</td>\n",
       "      <td>0.292875</td>\n",
       "      <td>0.313125</td>\n",
       "      <td>0.321000</td>\n",
       "      <td>0.314625</td>\n",
       "      <td>0.321375</td>\n",
       "      <td>0.318375</td>\n",
       "      <td>0.303375</td>\n",
       "      <td>0.288375</td>\n",
       "      <td>...</td>\n",
       "      <td>0.263250</td>\n",
       "      <td>0.257250</td>\n",
       "      <td>0.265125</td>\n",
       "      <td>0.253875</td>\n",
       "      <td>0.247500</td>\n",
       "      <td>0.258000</td>\n",
       "      <td>0.247875</td>\n",
       "      <td>0.237750</td>\n",
       "      <td>0.252000</td>\n",
       "      <td>0.286125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>站点20</td>\n",
       "      <td>0.288375</td>\n",
       "      <td>0.316500</td>\n",
       "      <td>0.320250</td>\n",
       "      <td>0.314250</td>\n",
       "      <td>0.322500</td>\n",
       "      <td>0.318000</td>\n",
       "      <td>0.300750</td>\n",
       "      <td>0.283875</td>\n",
       "      <td>...</td>\n",
       "      <td>0.264000</td>\n",
       "      <td>0.256875</td>\n",
       "      <td>0.261750</td>\n",
       "      <td>0.258375</td>\n",
       "      <td>0.253125</td>\n",
       "      <td>0.259125</td>\n",
       "      <td>0.252000</td>\n",
       "      <td>0.240000</td>\n",
       "      <td>0.256125</td>\n",
       "      <td>0.285375</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         Time MeasName        H0        H1        H2        H3        H4  \\\n",
       "0  2020-01-01      站点4  0.417375  0.431625  0.437625  0.439500  0.447375   \n",
       "1  2020-01-01      站点7  0.292375  0.321250  0.327250  0.324625  0.331750   \n",
       "2  2020-01-01     站点22  0.255250  0.307000  0.327625  0.323500  0.343000   \n",
       "3  2020-01-01     站点21  0.292875  0.313125  0.321000  0.314625  0.321375   \n",
       "4  2020-01-01     站点20  0.288375  0.316500  0.320250  0.314250  0.322500   \n",
       "\n",
       "         H5        H6        H7  ...       H14       H15       H16       H17  \\\n",
       "0  0.445125  0.436500  0.422250  ...  0.337875  0.336000  0.339750  0.327000   \n",
       "1  0.316750  0.309250  0.298750  ...  0.268000  0.267250  0.264625  0.261625   \n",
       "2  0.314500  0.304000  0.259375  ...  0.231625  0.226375  0.228250  0.229000   \n",
       "3  0.318375  0.303375  0.288375  ...  0.263250  0.257250  0.265125  0.253875   \n",
       "4  0.318000  0.300750  0.283875  ...  0.264000  0.256875  0.261750  0.258375   \n",
       "\n",
       "        H18       H19       H20       H21       H22       H23  \n",
       "0  0.320250  0.330750  0.336375  0.326625  0.350625  0.386625  \n",
       "1  0.250375  0.256375  0.255625  0.237250  0.257875  0.291625  \n",
       "2  0.230500  0.226375  0.229375  0.226000  0.229750  0.239500  \n",
       "3  0.247500  0.258000  0.247875  0.237750  0.252000  0.286125  \n",
       "4  0.253125  0.259125  0.252000  0.240000  0.256125  0.285375  \n",
       "\n",
       "[5 rows x 26 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train2020.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>Time</th>\n",
       "      <th>MeasName</th>\n",
       "      <th>Hour</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>站点4</td>\n",
       "      <td>H0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>站点4</td>\n",
       "      <td>H1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>站点4</td>\n",
       "      <td>H2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>站点4</td>\n",
       "      <td>H3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>站点4</td>\n",
       "      <td>H4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id        Time MeasName Hour\n",
       "0   0  2020-02-03      站点4   H0\n",
       "1   1  2020-02-03      站点4   H1\n",
       "2   2  2020-02-03      站点4   H2\n",
       "3   3  2020-02-03      站点4   H3\n",
       "4   4  2020-02-03      站点4   H4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>pressure</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.4335</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0.4335</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>0.4335</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>0.4335</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>0.4335</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  pressure\n",
       "0   0    0.4335\n",
       "1   1    0.4335\n",
       "2   2    0.4335\n",
       "3   3    0.4335\n",
       "4   4    0.4335"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "submit.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reshape_data(df1):\n",
    "    time = df1[\"Time\"].values\n",
    "    meas = df1[\"MeasName\"].values\n",
    "\n",
    "    df_list = []\n",
    "\n",
    "    for i in range(0,24):\n",
    "        hour = \"H\"+str(i)\n",
    "        pressure = df1[hour].values\n",
    "        df2 = pd.DataFrame()\n",
    "        df2[\"Time\"] = time\n",
    "        df2[\"MeasName\"] = meas\n",
    "        df2[\"Hour\"] = hour\n",
    "        df2[\"pressure\"] = pressure\n",
    "\n",
    "        df_list.append(df2)\n",
    "\n",
    "    df3 = pd.concat(df_list)\n",
    "    df3.sort_values(by = ['Time', 'MeasName'], inplace = True)\n",
    "    df3 = df3.reset_index(drop=True)\n",
    "    return df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "train2018 = reshape_data(train2018)\n",
    "train2019 = reshape_data(train2019)\n",
    "train2020 = reshape_data(train2020)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Time</th>\n",
       "      <th>MeasName</th>\n",
       "      <th>Hour</th>\n",
       "      <th>pressure</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>站点1</td>\n",
       "      <td>H0</td>\n",
       "      <td>0.288625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>站点1</td>\n",
       "      <td>H1</td>\n",
       "      <td>0.292000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>站点1</td>\n",
       "      <td>H2</td>\n",
       "      <td>0.290500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>站点1</td>\n",
       "      <td>H3</td>\n",
       "      <td>0.299500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>站点1</td>\n",
       "      <td>H4</td>\n",
       "      <td>0.300250</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Time MeasName Hour  pressure\n",
       "0  2018-01-01      站点1   H0  0.288625\n",
       "1  2018-01-01      站点1   H1  0.292000\n",
       "2  2018-01-01      站点1   H2  0.290500\n",
       "3  2018-01-01      站点1   H3  0.299500\n",
       "4  2018-01-01      站点1   H4  0.300250"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train2018.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "train2018['Time_time'] = pd.to_datetime(train2018['Time'])\n",
    "train2019['Time_time'] = pd.to_datetime(train2019['Time'])\n",
    "train2020['Time_time'] = pd.to_datetime(train2020['Time'])\n",
    "test['Time_time'] = pd.to_datetime(test['Time'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def abnormal(df):\n",
    "    '''\n",
    "    处理-9999异常值: 上一个值填充\n",
    "    '''\n",
    "    index_value = list(df[df['pressure'] == -99999].index)\n",
    "    for i in index_value:\n",
    "        value = df[df.index== (i - 1)]['pressure'].iloc[0]\n",
    "        df.loc[i, 'pressure'] = value\n",
    "    return df\n",
    "train2018 = abnormal(train2018)\n",
    "train2019 = abnormal(train2019)\n",
    "train2020 = abnormal(train2020)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def feature1(df):\n",
    "    df['Day'] = df['Time'].apply(lambda x: int(x.split('-')[-1]))\n",
    "    df['Hour'] = df['Hour'].apply(lambda x: int(x.replace('H', '')))\n",
    "    df['MeasName'] = df['MeasName'].apply(lambda x: int(x.replace('站点', '')))\n",
    "    \n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "train2018 = feature1(train2018)\n",
    "train2019 = feature1(train2019)\n",
    "train2020 = feature1(train2020)\n",
    "test = feature1(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Time</th>\n",
       "      <th>MeasName</th>\n",
       "      <th>Hour</th>\n",
       "      <th>pressure</th>\n",
       "      <th>Time_time</th>\n",
       "      <th>Day</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.309625</td>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.325750</td>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.332875</td>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.324250</td>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0.331750</td>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Time  MeasName  Hour  pressure  Time_time  Day\n",
       "0  2020-01-01         1     0  0.309625 2020-01-01    1\n",
       "1  2020-01-01         1     1  0.325750 2020-01-01    1\n",
       "2  2020-01-01         1     2  0.332875 2020-01-01    1\n",
       "3  2020-01-01         1     3  0.324250 2020-01-01    1\n",
       "4  2020-01-01         1     4  0.331750 2020-01-01    1"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train2020.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 训练集、测试集"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "训练数据 2018.1.1 - 2019.12.31\n",
    "\n",
    "验证数据 2020.1.1 - 2020.1.31；2020.3.1 - 2020.3.31；2020.5.1 - 2020.5.31；2020.8.1 - 2020.8.31\n",
    "\n",
    "测试数据 2020.2.3 - 2020.2.16；2020.4.6 - 2020.4.19；2020.6.1 - 2020.6.14；2020.9.7 - 2020.9.20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "分段1 训练集、测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Time</th>\n",
       "      <th>MeasName</th>\n",
       "      <th>Hour</th>\n",
       "      <th>pressure</th>\n",
       "      <th>Time_time</th>\n",
       "      <th>Day</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.225625</td>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.246250</td>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.259000</td>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.251875</td>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0.265750</td>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Time  MeasName  Hour  pressure  Time_time  Day\n",
       "0  2019-01-01         1     0  0.225625 2019-01-01    1\n",
       "1  2019-01-01         1     1  0.246250 2019-01-01    1\n",
       "2  2019-01-01         1     2  0.259000 2019-01-01    1\n",
       "3  2019-01-01         1     3  0.251875 2019-01-01    1\n",
       "4  2019-01-01         1     4  0.265750 2019-01-01    1"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train2019.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "feat nums  3 ['MeasName', 'Hour', 'Day']\n"
     ]
    }
   ],
   "source": [
    "train2019Mon2 = train2019[(train2019['Time_time'] >= '2019-2-1') & (train2019['Time_time'] <= '2019-2-28')]\n",
    "train2019Mon1 = train2019[(train2019['Time_time'] >= '2019-1-1') & (train2019['Time_time'] <= '2019-1-28')]\n",
    "Mon_2_1_2019 = train2019Mon2['pressure'].mean() - train2019Mon1['pressure'].mean()\n",
    "\n",
    "train1 = train2020[(train2020['Time_time'] >= '2020-1-1') & (train2020['Time_time'] <= '2020-1-31')]\n",
    "test1 = test[(test['Time_time'] >= '2020-2-3') & (test['Time_time'] <= '2020-2-16')]\n",
    "\n",
    "\n",
    "used_feat = [f for f in train1.columns if f not in ['id', 'pressure', 'Time', 'Time_time']]\n",
    "print('feat nums ', len(used_feat), used_feat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Time</th>\n",
       "      <th>MeasName</th>\n",
       "      <th>Hour</th>\n",
       "      <th>pressure</th>\n",
       "      <th>Time_time</th>\n",
       "      <th>Day</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.309625</td>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.325750</td>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.332875</td>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.324250</td>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0.331750</td>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Time  MeasName  Hour  pressure  Time_time  Day\n",
       "0  2020-01-01         1     0  0.309625 2020-01-01    1\n",
       "1  2020-01-01         1     1  0.325750 2020-01-01    1\n",
       "2  2020-01-01         1     2  0.332875 2020-01-01    1\n",
       "3  2020-01-01         1     3  0.324250 2020-01-01    1\n",
       "4  2020-01-01         1     4  0.331750 2020-01-01    1"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(22320, 3) (10080, 3)\n",
      "fold  1\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0173142\tvalid_1's l1: 0.0169753\n",
      "[100]\ttraining's l1: 0.0147663\tvalid_1's l1: 0.0145043\n",
      "[150]\ttraining's l1: 0.01405\tvalid_1's l1: 0.0138525\n",
      "[200]\ttraining's l1: 0.0137465\tvalid_1's l1: 0.0135758\n",
      "[250]\ttraining's l1: 0.0135952\tvalid_1's l1: 0.0134684\n",
      "[300]\ttraining's l1: 0.0135064\tvalid_1's l1: 0.0134122\n",
      "[350]\ttraining's l1: 0.0134427\tvalid_1's l1: 0.0133583\n",
      "[400]\ttraining's l1: 0.0133877\tvalid_1's l1: 0.0133215\n",
      "[450]\ttraining's l1: 0.0133501\tvalid_1's l1: 0.0132927\n",
      "[500]\ttraining's l1: 0.0133263\tvalid_1's l1: 0.0132767\n",
      "[550]\ttraining's l1: 0.0133091\tvalid_1's l1: 0.013259\n",
      "[600]\ttraining's l1: 0.0132958\tvalid_1's l1: 0.0132594\n",
      "Early stopping, best iteration is:\n",
      "[563]\ttraining's l1: 0.0133034\tvalid_1's l1: 0.0132544\n",
      "fold  2\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0177608\tvalid_1's l1: 0.0179471\n",
      "[100]\ttraining's l1: 0.0145848\tvalid_1's l1: 0.0149731\n",
      "[150]\ttraining's l1: 0.0138839\tvalid_1's l1: 0.0143693\n",
      "[200]\ttraining's l1: 0.0136176\tvalid_1's l1: 0.014151\n",
      "[250]\ttraining's l1: 0.0134353\tvalid_1's l1: 0.0139908\n",
      "[300]\ttraining's l1: 0.0133488\tvalid_1's l1: 0.0139183\n",
      "[350]\ttraining's l1: 0.0132947\tvalid_1's l1: 0.0138758\n",
      "[400]\ttraining's l1: 0.0132539\tvalid_1's l1: 0.0138581\n",
      "[450]\ttraining's l1: 0.0132216\tvalid_1's l1: 0.0138507\n",
      "[500]\ttraining's l1: 0.0131896\tvalid_1's l1: 0.0138369\n",
      "[550]\ttraining's l1: 0.0131695\tvalid_1's l1: 0.0138281\n",
      "[600]\ttraining's l1: 0.0131517\tvalid_1's l1: 0.0138234\n",
      "[650]\ttraining's l1: 0.0131373\tvalid_1's l1: 0.0138082\n",
      "[700]\ttraining's l1: 0.0131227\tvalid_1's l1: 0.013802\n",
      "[750]\ttraining's l1: 0.0131118\tvalid_1's l1: 0.0137944\n",
      "Early stopping, best iteration is:\n",
      "[746]\ttraining's l1: 0.0131124\tvalid_1's l1: 0.0137925\n",
      "fold  3\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0182951\tvalid_1's l1: 0.0187097\n",
      "[100]\ttraining's l1: 0.0146616\tvalid_1's l1: 0.0150491\n",
      "[150]\ttraining's l1: 0.0139462\tvalid_1's l1: 0.0142981\n",
      "[200]\ttraining's l1: 0.0136046\tvalid_1's l1: 0.0139718\n",
      "[250]\ttraining's l1: 0.0134798\tvalid_1's l1: 0.0138968\n",
      "[300]\ttraining's l1: 0.0133951\tvalid_1's l1: 0.0138389\n",
      "[350]\ttraining's l1: 0.0133253\tvalid_1's l1: 0.0137921\n",
      "[400]\ttraining's l1: 0.0132814\tvalid_1's l1: 0.0137618\n",
      "[450]\ttraining's l1: 0.0132484\tvalid_1's l1: 0.0137359\n",
      "[500]\ttraining's l1: 0.0132208\tvalid_1's l1: 0.0137186\n",
      "[550]\ttraining's l1: 0.013202\tvalid_1's l1: 0.0137171\n",
      "[600]\ttraining's l1: 0.0131884\tvalid_1's l1: 0.013716\n",
      "[650]\ttraining's l1: 0.0131754\tvalid_1's l1: 0.0137072\n",
      "[700]\ttraining's l1: 0.0131641\tvalid_1's l1: 0.013704\n",
      "Early stopping, best iteration is:\n",
      "[694]\ttraining's l1: 0.0131647\tvalid_1's l1: 0.0137035\n",
      "fold  4\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0176466\tvalid_1's l1: 0.0172108\n",
      "[100]\ttraining's l1: 0.0146937\tvalid_1's l1: 0.0141846\n",
      "[150]\ttraining's l1: 0.0140428\tvalid_1's l1: 0.0136209\n",
      "[200]\ttraining's l1: 0.0137429\tvalid_1's l1: 0.0133753\n",
      "[250]\ttraining's l1: 0.0136074\tvalid_1's l1: 0.013259\n",
      "[300]\ttraining's l1: 0.0135289\tvalid_1's l1: 0.0132016\n",
      "[350]\ttraining's l1: 0.0134688\tvalid_1's l1: 0.0131634\n",
      "[400]\ttraining's l1: 0.0134269\tvalid_1's l1: 0.0131312\n",
      "[450]\ttraining's l1: 0.0133989\tvalid_1's l1: 0.0131136\n",
      "[500]\ttraining's l1: 0.0133751\tvalid_1's l1: 0.0131039\n",
      "[550]\ttraining's l1: 0.0133555\tvalid_1's l1: 0.013099\n",
      "[600]\ttraining's l1: 0.013338\tvalid_1's l1: 0.0130828\n",
      "[650]\ttraining's l1: 0.0133231\tvalid_1's l1: 0.0130751\n",
      "[700]\ttraining's l1: 0.0133111\tvalid_1's l1: 0.0130665\n",
      "[750]\ttraining's l1: 0.0132984\tvalid_1's l1: 0.0130557\n",
      "[800]\ttraining's l1: 0.0132887\tvalid_1's l1: 0.0130532\n",
      "[850]\ttraining's l1: 0.0132808\tvalid_1's l1: 0.0130534\n",
      "Early stopping, best iteration is:\n",
      "[812]\ttraining's l1: 0.0132871\tvalid_1's l1: 0.0130492\n",
      "fold  5\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0176437\tvalid_1's l1: 0.0182069\n",
      "[100]\ttraining's l1: 0.0144959\tvalid_1's l1: 0.015287\n",
      "[150]\ttraining's l1: 0.0138329\tvalid_1's l1: 0.0146365\n",
      "[200]\ttraining's l1: 0.0135386\tvalid_1's l1: 0.0143381\n",
      "[250]\ttraining's l1: 0.0134025\tvalid_1's l1: 0.0142225\n",
      "[300]\ttraining's l1: 0.0133032\tvalid_1's l1: 0.0141292\n",
      "[350]\ttraining's l1: 0.01325\tvalid_1's l1: 0.0140846\n",
      "[400]\ttraining's l1: 0.0132107\tvalid_1's l1: 0.0140575\n",
      "[450]\ttraining's l1: 0.0131795\tvalid_1's l1: 0.0140388\n",
      "[500]\ttraining's l1: 0.0131499\tvalid_1's l1: 0.0140236\n",
      "[550]\ttraining's l1: 0.0131292\tvalid_1's l1: 0.0140115\n",
      "[600]\ttraining's l1: 0.0131115\tvalid_1's l1: 0.014006\n",
      "[650]\ttraining's l1: 0.0130968\tvalid_1's l1: 0.0139947\n",
      "[700]\ttraining's l1: 0.0130828\tvalid_1's l1: 0.0139902\n",
      "[750]\ttraining's l1: 0.0130715\tvalid_1's l1: 0.0139867\n",
      "[800]\ttraining's l1: 0.0130637\tvalid_1's l1: 0.0139854\n",
      "[850]\ttraining's l1: 0.0130528\tvalid_1's l1: 0.0139758\n",
      "[900]\ttraining's l1: 0.0130431\tvalid_1's l1: 0.0139688\n",
      "[950]\ttraining's l1: 0.0130353\tvalid_1's l1: 0.0139679\n",
      "Early stopping, best iteration is:\n",
      "[914]\ttraining's l1: 0.0130403\tvalid_1's l1: 0.0139619\n",
      "------------------------------------------------------------------------------------------------------------------------\n",
      "mse  0.0217\n",
      "fold  1\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0176878\tvalid_1's l1: 0.0179307\n",
      "[100]\ttraining's l1: 0.0146327\tvalid_1's l1: 0.0148394\n",
      "[150]\ttraining's l1: 0.0139188\tvalid_1's l1: 0.0141362\n",
      "[200]\ttraining's l1: 0.0136147\tvalid_1's l1: 0.0138541\n",
      "[250]\ttraining's l1: 0.0134884\tvalid_1's l1: 0.01375\n",
      "[300]\ttraining's l1: 0.0133942\tvalid_1's l1: 0.0136677\n",
      "[350]\ttraining's l1: 0.0133301\tvalid_1's l1: 0.0136288\n",
      "[400]\ttraining's l1: 0.0132865\tvalid_1's l1: 0.0135933\n",
      "[450]\ttraining's l1: 0.0132573\tvalid_1's l1: 0.013579\n",
      "[500]\ttraining's l1: 0.0132361\tvalid_1's l1: 0.0135539\n",
      "[550]\ttraining's l1: 0.0132141\tvalid_1's l1: 0.0135387\n",
      "[600]\ttraining's l1: 0.0131975\tvalid_1's l1: 0.0135353\n",
      "[650]\ttraining's l1: 0.0131844\tvalid_1's l1: 0.0135254\n",
      "[700]\ttraining's l1: 0.0131702\tvalid_1's l1: 0.0135222\n",
      "[750]\ttraining's l1: 0.0131597\tvalid_1's l1: 0.013525\n",
      "Early stopping, best iteration is:\n",
      "[715]\ttraining's l1: 0.0131675\tvalid_1's l1: 0.0135208\n",
      "fold  2\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0176697\tvalid_1's l1: 0.0178779\n",
      "[100]\ttraining's l1: 0.0145916\tvalid_1's l1: 0.0146999\n",
      "[150]\ttraining's l1: 0.0139125\tvalid_1's l1: 0.0140642\n",
      "[200]\ttraining's l1: 0.0136522\tvalid_1's l1: 0.0138341\n",
      "[250]\ttraining's l1: 0.0134973\tvalid_1's l1: 0.0136953\n",
      "[300]\ttraining's l1: 0.0134141\tvalid_1's l1: 0.0136352\n",
      "[350]\ttraining's l1: 0.0133535\tvalid_1's l1: 0.0135945\n",
      "[400]\ttraining's l1: 0.0133059\tvalid_1's l1: 0.0135636\n",
      "[450]\ttraining's l1: 0.0132771\tvalid_1's l1: 0.0135578\n",
      "[500]\ttraining's l1: 0.0132549\tvalid_1's l1: 0.0135515\n",
      "[550]\ttraining's l1: 0.0132382\tvalid_1's l1: 0.013537\n",
      "[600]\ttraining's l1: 0.013222\tvalid_1's l1: 0.0135279\n",
      "[650]\ttraining's l1: 0.0132075\tvalid_1's l1: 0.0135262\n",
      "Early stopping, best iteration is:\n",
      "[638]\ttraining's l1: 0.0132119\tvalid_1's l1: 0.0135236\n",
      "fold  3\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0175177\tvalid_1's l1: 0.0175039\n",
      "[100]\ttraining's l1: 0.0146369\tvalid_1's l1: 0.0149019\n",
      "[150]\ttraining's l1: 0.0139156\tvalid_1's l1: 0.0142225\n",
      "[200]\ttraining's l1: 0.013603\tvalid_1's l1: 0.0139702\n",
      "[250]\ttraining's l1: 0.0134562\tvalid_1's l1: 0.0138421\n",
      "[300]\ttraining's l1: 0.0133657\tvalid_1's l1: 0.0137468\n",
      "[350]\ttraining's l1: 0.0133099\tvalid_1's l1: 0.0137188\n",
      "[400]\ttraining's l1: 0.0132677\tvalid_1's l1: 0.0136849\n",
      "[450]\ttraining's l1: 0.0132388\tvalid_1's l1: 0.013675\n",
      "[500]\ttraining's l1: 0.0132149\tvalid_1's l1: 0.0136645\n",
      "[550]\ttraining's l1: 0.0131955\tvalid_1's l1: 0.0136523\n",
      "[600]\ttraining's l1: 0.0131783\tvalid_1's l1: 0.013634\n",
      "[650]\ttraining's l1: 0.0131663\tvalid_1's l1: 0.0136257\n",
      "Early stopping, best iteration is:\n",
      "[640]\ttraining's l1: 0.0131679\tvalid_1's l1: 0.0136253\n",
      "fold  4\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.018105\tvalid_1's l1: 0.0183694\n",
      "[100]\ttraining's l1: 0.0145776\tvalid_1's l1: 0.0149619\n",
      "[150]\ttraining's l1: 0.0139148\tvalid_1's l1: 0.0142997\n",
      "[200]\ttraining's l1: 0.0136029\tvalid_1's l1: 0.0139947\n",
      "[250]\ttraining's l1: 0.0134651\tvalid_1's l1: 0.0138645\n",
      "[300]\ttraining's l1: 0.0133704\tvalid_1's l1: 0.0137919\n",
      "[350]\ttraining's l1: 0.0133103\tvalid_1's l1: 0.0137519\n",
      "[400]\ttraining's l1: 0.0132681\tvalid_1's l1: 0.0137261\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[450]\ttraining's l1: 0.0132413\tvalid_1's l1: 0.0137042\n",
      "[500]\ttraining's l1: 0.0132185\tvalid_1's l1: 0.0136945\n",
      "[550]\ttraining's l1: 0.0131965\tvalid_1's l1: 0.0136771\n",
      "[600]\ttraining's l1: 0.0131779\tvalid_1's l1: 0.0136626\n",
      "[650]\ttraining's l1: 0.013165\tvalid_1's l1: 0.0136518\n",
      "[700]\ttraining's l1: 0.0131538\tvalid_1's l1: 0.0136475\n",
      "[750]\ttraining's l1: 0.0131479\tvalid_1's l1: 0.0136453\n",
      "[800]\ttraining's l1: 0.0131368\tvalid_1's l1: 0.0136407\n",
      "[850]\ttraining's l1: 0.0131301\tvalid_1's l1: 0.0136391\n",
      "[900]\ttraining's l1: 0.0131234\tvalid_1's l1: 0.013639\n",
      "[950]\ttraining's l1: 0.013117\tvalid_1's l1: 0.0136389\n",
      "Early stopping, best iteration is:\n",
      "[928]\ttraining's l1: 0.0131194\tvalid_1's l1: 0.0136361\n",
      "fold  5\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0177235\tvalid_1's l1: 0.0174139\n",
      "[100]\ttraining's l1: 0.0146986\tvalid_1's l1: 0.0144505\n",
      "[150]\ttraining's l1: 0.0140149\tvalid_1's l1: 0.0138397\n",
      "[200]\ttraining's l1: 0.0137116\tvalid_1's l1: 0.0135708\n",
      "[250]\ttraining's l1: 0.0135724\tvalid_1's l1: 0.0134934\n",
      "[300]\ttraining's l1: 0.0134851\tvalid_1's l1: 0.0134325\n",
      "[350]\ttraining's l1: 0.0134247\tvalid_1's l1: 0.0133963\n",
      "[400]\ttraining's l1: 0.013379\tvalid_1's l1: 0.0133673\n",
      "[450]\ttraining's l1: 0.0133443\tvalid_1's l1: 0.0133565\n",
      "[500]\ttraining's l1: 0.0133207\tvalid_1's l1: 0.0133402\n",
      "[550]\ttraining's l1: 0.0132984\tvalid_1's l1: 0.0133213\n",
      "[600]\ttraining's l1: 0.013278\tvalid_1's l1: 0.013313\n",
      "[650]\ttraining's l1: 0.0132658\tvalid_1's l1: 0.0133095\n",
      "[700]\ttraining's l1: 0.0132546\tvalid_1's l1: 0.0133028\n",
      "Early stopping, best iteration is:\n",
      "[684]\ttraining's l1: 0.0132583\tvalid_1's l1: 0.0133002\n",
      "------------------------------------------------------------------------------------------------------------------------\n",
      "mse  0.00109\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/sunzhongyu/opt/anaconda3/envs/python36/lib/python3.6/site-packages/ipykernel_launcher.py:53: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n"
     ]
    }
   ],
   "source": [
    "train_x = train1[used_feat]\n",
    "train_y = train1['pressure']\n",
    "test_x = test1[used_feat]\n",
    "print(train_x.shape, test_x.shape)\n",
    "\n",
    "# -----------------------------------------------\n",
    "scores = []\n",
    "\n",
    "params = {'learning_rate': 0.1, \n",
    "        'boosting_type': 'gbdt', \n",
    "        'objective': 'regression_l1',\n",
    "        'metric': 'mae',\n",
    "        'min_child_samples': 46, \n",
    "        'min_child_weight': 0.01,\n",
    "        'feature_fraction': 0.8, \n",
    "        'bagging_fraction': 0.8, \n",
    "        'bagging_freq': 2, \n",
    "        'num_leaves': 16, \n",
    "        'max_depth': 5, \n",
    "        'n_jobs': -1, \n",
    "        'seed': 2019, \n",
    "        'verbosity': -1, \n",
    "       }\n",
    "\n",
    "\n",
    "\n",
    "oof_train = np.zeros(len(train_x))\n",
    "preds = np.zeros(len(test_x))\n",
    "folds = 5\n",
    "seeds = [2048, 1997]\n",
    "for seed in seeds: \n",
    "    kfold = KFold(n_splits=folds, shuffle=True, random_state=seed)\n",
    "    for fold, (trn_idx, val_idx) in enumerate(kfold.split(train_x, train_y)):\n",
    "        print('fold ', fold + 1)\n",
    "        x_trn, y_trn, x_val, y_val = train_x.iloc[trn_idx], train_y.iloc[trn_idx], train_x.iloc[val_idx], train_y.iloc[val_idx]\n",
    "        train_set = lgb.Dataset(x_trn, y_trn)\n",
    "        val_set = lgb.Dataset(x_val, y_val)\n",
    "\n",
    "        model = lgb.train(params, train_set, num_boost_round=500000,\n",
    "                          valid_sets=(train_set, val_set), early_stopping_rounds=50,\n",
    "                          verbose_eval=50)\n",
    "        oof_train[val_idx] += model.predict(x_val) / len(seeds)\n",
    "        preds += model.predict(test_x) / folds / len(seeds)\n",
    "        del x_trn, y_trn, x_val, y_val, model, train_set, val_set\n",
    "        gc.collect()\n",
    "    \n",
    "    mse = (mean_squared_error(oof_train, train1['pressure']))\n",
    "    \n",
    "    print('-'*120)\n",
    "    print('mse ', round(mse, 5))\n",
    "\n",
    "\n",
    "test1['pressure'] = preds + Mon_2_1_2019\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "分段2 训练集、测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "feat nums  3 ['MeasName', 'Hour', 'Day']\n"
     ]
    }
   ],
   "source": [
    "train2019Mon4 = train2019[(train2019['Time_time'] >= '2019-4-1') & (train2019['Time_time'] <= '2019-4-30')]\n",
    "train2019Mon3 = train2019[(train2019['Time_time'] >= '2019-3-1') & (train2019['Time_time'] <= '2019-3-30')]\n",
    "Mon_4_3_2019 = train2019Mon4['pressure'].mean() - train2019Mon3['pressure'].mean()\n",
    "\n",
    "\n",
    "train2 = train2020[(train2020['Time_time'] >= '2020-3-1') & (train2020['Time_time'] <= '2020-3-31')]\n",
    "test2 = test[(test['Time_time'] >= '2020-4-6') & (test['Time_time'] <= '2020-4-19')]\n",
    "\n",
    "\n",
    "used_feat = [f for f in train2.columns if f not in ['id', 'pressure', 'Time', 'Time_time']]\n",
    "print('feat nums ', len(used_feat), used_feat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(22320, 3) (10080, 3)\n",
      "fold  1\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.018869\tvalid_1's l1: 0.0174423\n",
      "[100]\ttraining's l1: 0.0158103\tvalid_1's l1: 0.0144793\n",
      "[150]\ttraining's l1: 0.0151531\tvalid_1's l1: 0.0139297\n",
      "[200]\ttraining's l1: 0.0148434\tvalid_1's l1: 0.0136581\n",
      "[250]\ttraining's l1: 0.0146681\tvalid_1's l1: 0.0135006\n",
      "[300]\ttraining's l1: 0.0145683\tvalid_1's l1: 0.0134137\n",
      "[350]\ttraining's l1: 0.0144844\tvalid_1's l1: 0.0133426\n",
      "[400]\ttraining's l1: 0.0144234\tvalid_1's l1: 0.0132958\n",
      "[450]\ttraining's l1: 0.0143911\tvalid_1's l1: 0.0132721\n",
      "[500]\ttraining's l1: 0.0143568\tvalid_1's l1: 0.0132578\n",
      "[550]\ttraining's l1: 0.0143307\tvalid_1's l1: 0.0132427\n",
      "[600]\ttraining's l1: 0.0143101\tvalid_1's l1: 0.0132226\n",
      "[650]\ttraining's l1: 0.0142947\tvalid_1's l1: 0.0132127\n",
      "[700]\ttraining's l1: 0.0142772\tvalid_1's l1: 0.0132037\n",
      "[750]\ttraining's l1: 0.0142599\tvalid_1's l1: 0.0131965\n",
      "[800]\ttraining's l1: 0.0142469\tvalid_1's l1: 0.0131823\n",
      "[850]\ttraining's l1: 0.0142348\tvalid_1's l1: 0.0131728\n",
      "[900]\ttraining's l1: 0.0142256\tvalid_1's l1: 0.0131663\n",
      "[950]\ttraining's l1: 0.014217\tvalid_1's l1: 0.0131599\n",
      "[1000]\ttraining's l1: 0.0142102\tvalid_1's l1: 0.0131545\n",
      "[1050]\ttraining's l1: 0.0142009\tvalid_1's l1: 0.0131479\n",
      "[1100]\ttraining's l1: 0.0141946\tvalid_1's l1: 0.0131462\n",
      "Early stopping, best iteration is:\n",
      "[1093]\ttraining's l1: 0.0141949\tvalid_1's l1: 0.0131436\n",
      "fold  2\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0184782\tvalid_1's l1: 0.017985\n",
      "[100]\ttraining's l1: 0.015602\tvalid_1's l1: 0.0151617\n",
      "[150]\ttraining's l1: 0.0150011\tvalid_1's l1: 0.0146251\n",
      "[200]\ttraining's l1: 0.0146619\tvalid_1's l1: 0.0143009\n",
      "[250]\ttraining's l1: 0.0144843\tvalid_1's l1: 0.014136\n",
      "[300]\ttraining's l1: 0.0143885\tvalid_1's l1: 0.0140493\n",
      "[350]\ttraining's l1: 0.0143101\tvalid_1's l1: 0.0139757\n",
      "[400]\ttraining's l1: 0.0142665\tvalid_1's l1: 0.0139392\n",
      "[450]\ttraining's l1: 0.0142218\tvalid_1's l1: 0.0139022\n",
      "[500]\ttraining's l1: 0.0141866\tvalid_1's l1: 0.0138773\n",
      "[550]\ttraining's l1: 0.0141617\tvalid_1's l1: 0.0138688\n",
      "[600]\ttraining's l1: 0.0141349\tvalid_1's l1: 0.0138572\n",
      "[650]\ttraining's l1: 0.0141128\tvalid_1's l1: 0.0138413\n",
      "[700]\ttraining's l1: 0.0140963\tvalid_1's l1: 0.0138316\n",
      "[750]\ttraining's l1: 0.0140836\tvalid_1's l1: 0.0138244\n",
      "[800]\ttraining's l1: 0.0140713\tvalid_1's l1: 0.0138162\n",
      "[850]\ttraining's l1: 0.0140596\tvalid_1's l1: 0.0138091\n",
      "[900]\ttraining's l1: 0.0140503\tvalid_1's l1: 0.0138056\n",
      "[950]\ttraining's l1: 0.0140412\tvalid_1's l1: 0.0137953\n",
      "[1000]\ttraining's l1: 0.0140332\tvalid_1's l1: 0.0137941\n",
      "[1050]\ttraining's l1: 0.0140267\tvalid_1's l1: 0.0137958\n",
      "Early stopping, best iteration is:\n",
      "[1030]\ttraining's l1: 0.0140289\tvalid_1's l1: 0.0137909\n",
      "fold  3\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.017999\tvalid_1's l1: 0.0191102\n",
      "[100]\ttraining's l1: 0.0152702\tvalid_1's l1: 0.0163817\n",
      "[150]\ttraining's l1: 0.014598\tvalid_1's l1: 0.0157659\n",
      "[200]\ttraining's l1: 0.0142862\tvalid_1's l1: 0.015527\n",
      "[250]\ttraining's l1: 0.0140932\tvalid_1's l1: 0.0154052\n",
      "[300]\ttraining's l1: 0.0139694\tvalid_1's l1: 0.0153227\n",
      "[350]\ttraining's l1: 0.013906\tvalid_1's l1: 0.0152869\n",
      "[400]\ttraining's l1: 0.0138577\tvalid_1's l1: 0.0152623\n",
      "[450]\ttraining's l1: 0.0138268\tvalid_1's l1: 0.0152416\n",
      "[500]\ttraining's l1: 0.0137978\tvalid_1's l1: 0.0152328\n",
      "[550]\ttraining's l1: 0.013774\tvalid_1's l1: 0.0152186\n",
      "[600]\ttraining's l1: 0.0137537\tvalid_1's l1: 0.0152056\n",
      "[650]\ttraining's l1: 0.01374\tvalid_1's l1: 0.0152006\n",
      "[700]\ttraining's l1: 0.0137258\tvalid_1's l1: 0.0151892\n",
      "[750]\ttraining's l1: 0.0137128\tvalid_1's l1: 0.0151791\n",
      "[800]\ttraining's l1: 0.0137037\tvalid_1's l1: 0.0151721\n",
      "[850]\ttraining's l1: 0.0136957\tvalid_1's l1: 0.0151699\n",
      "[900]\ttraining's l1: 0.013689\tvalid_1's l1: 0.0151721\n",
      "Early stopping, best iteration is:\n",
      "[880]\ttraining's l1: 0.0136919\tvalid_1's l1: 0.0151692\n",
      "fold  4\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0182086\tvalid_1's l1: 0.0192722\n",
      "[100]\ttraining's l1: 0.0153661\tvalid_1's l1: 0.0162051\n",
      "[150]\ttraining's l1: 0.0146915\tvalid_1's l1: 0.0155162\n",
      "[200]\ttraining's l1: 0.0144042\tvalid_1's l1: 0.0152363\n",
      "[250]\ttraining's l1: 0.0142197\tvalid_1's l1: 0.0150606\n",
      "[300]\ttraining's l1: 0.0141169\tvalid_1's l1: 0.0149738\n",
      "[350]\ttraining's l1: 0.0140513\tvalid_1's l1: 0.0149255\n",
      "[400]\ttraining's l1: 0.0140034\tvalid_1's l1: 0.0148874\n",
      "[450]\ttraining's l1: 0.0139588\tvalid_1's l1: 0.0148525\n",
      "[500]\ttraining's l1: 0.0139244\tvalid_1's l1: 0.0148282\n",
      "[550]\ttraining's l1: 0.0139043\tvalid_1's l1: 0.0148094\n",
      "[600]\ttraining's l1: 0.0138853\tvalid_1's l1: 0.0147995\n",
      "[650]\ttraining's l1: 0.0138681\tvalid_1's l1: 0.0147859\n",
      "[700]\ttraining's l1: 0.0138497\tvalid_1's l1: 0.0147711\n",
      "[750]\ttraining's l1: 0.0138358\tvalid_1's l1: 0.0147565\n",
      "[800]\ttraining's l1: 0.0138242\tvalid_1's l1: 0.0147496\n",
      "[850]\ttraining's l1: 0.0138151\tvalid_1's l1: 0.0147436\n",
      "[900]\ttraining's l1: 0.0138044\tvalid_1's l1: 0.0147376\n",
      "[950]\ttraining's l1: 0.0137972\tvalid_1's l1: 0.014734\n",
      "[1000]\ttraining's l1: 0.0137904\tvalid_1's l1: 0.0147305\n",
      "[1050]\ttraining's l1: 0.013785\tvalid_1's l1: 0.0147226\n",
      "[1100]\ttraining's l1: 0.013779\tvalid_1's l1: 0.0147177\n",
      "Early stopping, best iteration is:\n",
      "[1092]\ttraining's l1: 0.0137799\tvalid_1's l1: 0.0147148\n",
      "fold  5\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0182696\tvalid_1's l1: 0.0187514\n",
      "[100]\ttraining's l1: 0.0155026\tvalid_1's l1: 0.0161198\n",
      "[150]\ttraining's l1: 0.014808\tvalid_1's l1: 0.01545\n",
      "[200]\ttraining's l1: 0.0144439\tvalid_1's l1: 0.0150682\n",
      "[250]\ttraining's l1: 0.0142699\tvalid_1's l1: 0.0149126\n",
      "[300]\ttraining's l1: 0.0141574\tvalid_1's l1: 0.0147892\n",
      "[350]\ttraining's l1: 0.0141051\tvalid_1's l1: 0.0147493\n",
      "[400]\ttraining's l1: 0.0140597\tvalid_1's l1: 0.0147122\n",
      "[450]\ttraining's l1: 0.014019\tvalid_1's l1: 0.0146789\n",
      "[500]\ttraining's l1: 0.0139836\tvalid_1's l1: 0.0146472\n",
      "[550]\ttraining's l1: 0.0139633\tvalid_1's l1: 0.0146359\n",
      "[600]\ttraining's l1: 0.0139428\tvalid_1's l1: 0.0146164\n",
      "[650]\ttraining's l1: 0.0139288\tvalid_1's l1: 0.0146086\n",
      "[700]\ttraining's l1: 0.0139132\tvalid_1's l1: 0.0145968\n",
      "[750]\ttraining's l1: 0.0139011\tvalid_1's l1: 0.0145884\n",
      "[800]\ttraining's l1: 0.0138873\tvalid_1's l1: 0.014573\n",
      "[850]\ttraining's l1: 0.0138763\tvalid_1's l1: 0.0145684\n",
      "[900]\ttraining's l1: 0.0138674\tvalid_1's l1: 0.0145599\n",
      "[950]\ttraining's l1: 0.0138598\tvalid_1's l1: 0.0145561\n",
      "[1000]\ttraining's l1: 0.0138538\tvalid_1's l1: 0.0145536\n",
      "[1050]\ttraining's l1: 0.0138478\tvalid_1's l1: 0.0145447\n",
      "[1100]\ttraining's l1: 0.0138428\tvalid_1's l1: 0.0145456\n",
      "Early stopping, best iteration is:\n",
      "[1052]\ttraining's l1: 0.0138475\tvalid_1's l1: 0.0145441\n",
      "------------------------------------------------------------------------------------------------------------------------\n",
      "mse  0.02332\n",
      "fold  1\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0180937\tvalid_1's l1: 0.0201925\n",
      "[100]\ttraining's l1: 0.015148\tvalid_1's l1: 0.01716\n",
      "[150]\ttraining's l1: 0.0143917\tvalid_1's l1: 0.016446\n",
      "[200]\ttraining's l1: 0.0141427\tvalid_1's l1: 0.0162433\n",
      "[250]\ttraining's l1: 0.0139882\tvalid_1's l1: 0.0161096\n",
      "[300]\ttraining's l1: 0.0138823\tvalid_1's l1: 0.0160129\n",
      "[350]\ttraining's l1: 0.0138052\tvalid_1's l1: 0.0159463\n",
      "[400]\ttraining's l1: 0.0137549\tvalid_1's l1: 0.0158929\n",
      "[450]\ttraining's l1: 0.0137186\tvalid_1's l1: 0.0158624\n",
      "[500]\ttraining's l1: 0.0136827\tvalid_1's l1: 0.0158336\n",
      "[550]\ttraining's l1: 0.0136612\tvalid_1's l1: 0.0158169\n",
      "[600]\ttraining's l1: 0.0136418\tvalid_1's l1: 0.0158044\n",
      "[650]\ttraining's l1: 0.0136262\tvalid_1's l1: 0.0158034\n",
      "Early stopping, best iteration is:\n",
      "[606]\ttraining's l1: 0.0136394\tvalid_1's l1: 0.0158017\n",
      "fold  2\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0189077\tvalid_1's l1: 0.0184336\n",
      "[100]\ttraining's l1: 0.0158148\tvalid_1's l1: 0.0153068\n",
      "[150]\ttraining's l1: 0.0150352\tvalid_1's l1: 0.0145113\n",
      "[200]\ttraining's l1: 0.014726\tvalid_1's l1: 0.0142247\n",
      "[250]\ttraining's l1: 0.0145106\tvalid_1's l1: 0.014046\n",
      "[300]\ttraining's l1: 0.0143839\tvalid_1's l1: 0.013951\n",
      "[350]\ttraining's l1: 0.0143105\tvalid_1's l1: 0.0138984\n",
      "[400]\ttraining's l1: 0.0142545\tvalid_1's l1: 0.0138586\n",
      "[450]\ttraining's l1: 0.0142149\tvalid_1's l1: 0.0138322\n",
      "[500]\ttraining's l1: 0.0141849\tvalid_1's l1: 0.0138102\n",
      "[550]\ttraining's l1: 0.0141567\tvalid_1's l1: 0.0137914\n",
      "[600]\ttraining's l1: 0.0141295\tvalid_1's l1: 0.0137764\n",
      "[650]\ttraining's l1: 0.0141105\tvalid_1's l1: 0.0137706\n",
      "[700]\ttraining's l1: 0.014094\tvalid_1's l1: 0.0137646\n",
      "[750]\ttraining's l1: 0.0140754\tvalid_1's l1: 0.0137635\n",
      "[800]\ttraining's l1: 0.0140646\tvalid_1's l1: 0.0137556\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[850]\ttraining's l1: 0.014055\tvalid_1's l1: 0.0137546\n",
      "Early stopping, best iteration is:\n",
      "[825]\ttraining's l1: 0.0140616\tvalid_1's l1: 0.0137519\n",
      "fold  3\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0182748\tvalid_1's l1: 0.0187511\n",
      "[100]\ttraining's l1: 0.015445\tvalid_1's l1: 0.0163186\n",
      "[150]\ttraining's l1: 0.0147685\tvalid_1's l1: 0.0157058\n",
      "[200]\ttraining's l1: 0.0144409\tvalid_1's l1: 0.015426\n",
      "[250]\ttraining's l1: 0.0142004\tvalid_1's l1: 0.0152056\n",
      "[300]\ttraining's l1: 0.0140901\tvalid_1's l1: 0.0151295\n",
      "[350]\ttraining's l1: 0.0140164\tvalid_1's l1: 0.015082\n",
      "[400]\ttraining's l1: 0.0139639\tvalid_1's l1: 0.0150425\n",
      "[450]\ttraining's l1: 0.0139207\tvalid_1's l1: 0.015006\n",
      "[500]\ttraining's l1: 0.0138937\tvalid_1's l1: 0.0149924\n",
      "[550]\ttraining's l1: 0.0138729\tvalid_1's l1: 0.0149747\n",
      "[600]\ttraining's l1: 0.0138496\tvalid_1's l1: 0.0149543\n",
      "[650]\ttraining's l1: 0.0138327\tvalid_1's l1: 0.0149397\n",
      "[700]\ttraining's l1: 0.013816\tvalid_1's l1: 0.0149327\n",
      "[750]\ttraining's l1: 0.0138031\tvalid_1's l1: 0.0149167\n",
      "[800]\ttraining's l1: 0.0137929\tvalid_1's l1: 0.0149059\n",
      "[850]\ttraining's l1: 0.0137829\tvalid_1's l1: 0.0149082\n",
      "Early stopping, best iteration is:\n",
      "[818]\ttraining's l1: 0.0137886\tvalid_1's l1: 0.014905\n",
      "fold  4\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0192124\tvalid_1's l1: 0.0169233\n",
      "[100]\ttraining's l1: 0.0161361\tvalid_1's l1: 0.0138578\n",
      "[150]\ttraining's l1: 0.0153359\tvalid_1's l1: 0.0130369\n",
      "[200]\ttraining's l1: 0.0150235\tvalid_1's l1: 0.0127463\n",
      "[250]\ttraining's l1: 0.0148289\tvalid_1's l1: 0.0126081\n",
      "[300]\ttraining's l1: 0.0147182\tvalid_1's l1: 0.0125221\n",
      "[350]\ttraining's l1: 0.0146447\tvalid_1's l1: 0.0124756\n",
      "[400]\ttraining's l1: 0.0145866\tvalid_1's l1: 0.0124424\n",
      "[450]\ttraining's l1: 0.0145472\tvalid_1's l1: 0.0124143\n",
      "[500]\ttraining's l1: 0.0145148\tvalid_1's l1: 0.0123991\n",
      "[550]\ttraining's l1: 0.0144838\tvalid_1's l1: 0.0123884\n",
      "[600]\ttraining's l1: 0.0144668\tvalid_1's l1: 0.0123814\n",
      "[650]\ttraining's l1: 0.0144522\tvalid_1's l1: 0.0123761\n",
      "[700]\ttraining's l1: 0.0144342\tvalid_1's l1: 0.0123657\n",
      "[750]\ttraining's l1: 0.0144226\tvalid_1's l1: 0.0123666\n",
      "[800]\ttraining's l1: 0.0144113\tvalid_1's l1: 0.0123616\n",
      "[850]\ttraining's l1: 0.0144027\tvalid_1's l1: 0.0123602\n",
      "[900]\ttraining's l1: 0.0143948\tvalid_1's l1: 0.0123545\n",
      "[950]\ttraining's l1: 0.0143863\tvalid_1's l1: 0.0123532\n",
      "[1000]\ttraining's l1: 0.0143779\tvalid_1's l1: 0.0123523\n",
      "Early stopping, best iteration is:\n",
      "[993]\ttraining's l1: 0.0143785\tvalid_1's l1: 0.0123507\n",
      "fold  5\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0183446\tvalid_1's l1: 0.0190184\n",
      "[100]\ttraining's l1: 0.0155237\tvalid_1's l1: 0.0161972\n",
      "[150]\ttraining's l1: 0.0148343\tvalid_1's l1: 0.0154821\n",
      "[200]\ttraining's l1: 0.0144652\tvalid_1's l1: 0.0151092\n",
      "[250]\ttraining's l1: 0.014308\tvalid_1's l1: 0.0149759\n",
      "[300]\ttraining's l1: 0.0141923\tvalid_1's l1: 0.0148797\n",
      "[350]\ttraining's l1: 0.0141174\tvalid_1's l1: 0.0148106\n",
      "[400]\ttraining's l1: 0.014065\tvalid_1's l1: 0.0147699\n",
      "[450]\ttraining's l1: 0.0140185\tvalid_1's l1: 0.0147295\n",
      "[500]\ttraining's l1: 0.0139909\tvalid_1's l1: 0.0147132\n",
      "[550]\ttraining's l1: 0.0139646\tvalid_1's l1: 0.0146981\n",
      "[600]\ttraining's l1: 0.0139452\tvalid_1's l1: 0.014678\n",
      "[650]\ttraining's l1: 0.0139256\tvalid_1's l1: 0.0146589\n",
      "[700]\ttraining's l1: 0.0139099\tvalid_1's l1: 0.0146421\n",
      "[750]\ttraining's l1: 0.0138924\tvalid_1's l1: 0.0146261\n",
      "[800]\ttraining's l1: 0.0138801\tvalid_1's l1: 0.0146207\n",
      "[850]\ttraining's l1: 0.0138689\tvalid_1's l1: 0.0146113\n",
      "Early stopping, best iteration is:\n",
      "[828]\ttraining's l1: 0.0138727\tvalid_1's l1: 0.0146071\n",
      "------------------------------------------------------------------------------------------------------------------------\n",
      "mse  0.00297\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/sunzhongyu/opt/anaconda3/envs/python36/lib/python3.6/site-packages/ipykernel_launcher.py:53: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n"
     ]
    }
   ],
   "source": [
    "train_x = train2[used_feat]\n",
    "train_y = train2['pressure']\n",
    "test_x = test2[used_feat]\n",
    "print(train_x.shape, test_x.shape)\n",
    "\n",
    "# -----------------------------------------------\n",
    "scores = []\n",
    "\n",
    "params = {'learning_rate': 0.1, \n",
    "        'boosting_type': 'gbdt', \n",
    "        'objective': 'regression_l1',\n",
    "        'metric': 'mae',\n",
    "        'min_child_samples': 46, \n",
    "        'min_child_weight': 0.01,\n",
    "        'feature_fraction': 0.8, \n",
    "        'bagging_fraction': 0.8, \n",
    "        'bagging_freq': 2, \n",
    "        'num_leaves': 16, \n",
    "        'max_depth': 5, \n",
    "        'n_jobs': -1, \n",
    "        'seed': 2019, \n",
    "        'verbosity': -1, \n",
    "       }\n",
    "\n",
    "\n",
    "\n",
    "oof_train = np.zeros(len(train_x))\n",
    "preds = np.zeros(len(test_x))\n",
    "folds = 5\n",
    "seeds = [2048, 1997] \n",
    "for seed in seeds: \n",
    "    kfold = KFold(n_splits=folds, shuffle=True, random_state=seed)\n",
    "    for fold, (trn_idx, val_idx) in enumerate(kfold.split(train_x, train_y)):\n",
    "        print('fold ', fold + 1)\n",
    "        x_trn, y_trn, x_val, y_val = train_x.iloc[trn_idx], train_y.iloc[trn_idx], train_x.iloc[val_idx], train_y.iloc[val_idx]\n",
    "        train_set = lgb.Dataset(x_trn, y_trn)\n",
    "        val_set = lgb.Dataset(x_val, y_val)\n",
    "\n",
    "        model = lgb.train(params, train_set, num_boost_round=500000,\n",
    "                          valid_sets=(train_set, val_set), early_stopping_rounds=50,\n",
    "                          verbose_eval=50)\n",
    "        oof_train[val_idx] += model.predict(x_val) / len(seeds)\n",
    "        preds += model.predict(test_x) / folds / len(seeds)\n",
    "        del x_trn, y_trn, x_val, y_val, model, train_set, val_set\n",
    "        gc.collect()\n",
    "    \n",
    "    mse = (mean_squared_error(oof_train, train2['pressure']))\n",
    "    \n",
    "    print('-'*120)\n",
    "    print('mse ', round(mse, 5))\n",
    "\n",
    "    \n",
    "test2['pressure'] = preds + Mon_4_3_2019\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "分段3 训练集、测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "feat nums  3 ['MeasName', 'Hour', 'Day']\n"
     ]
    }
   ],
   "source": [
    "train2019Mon6 = train2019[(train2019['Time_time'] >= '2019-6-1') & (train2019['Time_time'] <= '2019-6-30')]\n",
    "train2019Mon5 = train2019[(train2019['Time_time'] >= '2019-5-1') & (train2019['Time_time'] <= '2019-5-30')]\n",
    "Mon_6_5_2019 = train2019Mon6['pressure'].mean() - train2019Mon5['pressure'].mean()\n",
    "\n",
    "train3 = train2020[(train2020['Time_time'] >= '2020-5-1') & (train2020['Time_time'] <= '2020-5-31')]\n",
    "test3 = test[(test['Time_time'] >= '2020-6-1') & (test['Time_time'] <= '2020-6-14')]\n",
    "\n",
    "\n",
    "used_feat = [f for f in train3.columns if f not in ['id', 'pressure', 'Time', 'Time_time']]\n",
    "print('feat nums ', len(used_feat), used_feat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(22320, 3) (10080, 3)\n",
      "fold  1\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0188884\tvalid_1's l1: 0.0187197\n",
      "[100]\ttraining's l1: 0.015304\tvalid_1's l1: 0.0152618\n",
      "[150]\ttraining's l1: 0.0142656\tvalid_1's l1: 0.014327\n",
      "[200]\ttraining's l1: 0.0137474\tvalid_1's l1: 0.0138451\n",
      "[250]\ttraining's l1: 0.0134329\tvalid_1's l1: 0.0135748\n",
      "[300]\ttraining's l1: 0.013237\tvalid_1's l1: 0.0134216\n",
      "[350]\ttraining's l1: 0.0131166\tvalid_1's l1: 0.0133055\n",
      "[400]\ttraining's l1: 0.0130105\tvalid_1's l1: 0.0131886\n",
      "[450]\ttraining's l1: 0.0129464\tvalid_1's l1: 0.0131018\n",
      "[500]\ttraining's l1: 0.0129016\tvalid_1's l1: 0.0130665\n",
      "[550]\ttraining's l1: 0.0128681\tvalid_1's l1: 0.0130438\n",
      "[600]\ttraining's l1: 0.0128265\tvalid_1's l1: 0.0130029\n",
      "[650]\ttraining's l1: 0.0127987\tvalid_1's l1: 0.0129668\n",
      "[700]\ttraining's l1: 0.0127758\tvalid_1's l1: 0.0129468\n",
      "[750]\ttraining's l1: 0.0127505\tvalid_1's l1: 0.0129273\n",
      "[800]\ttraining's l1: 0.0127302\tvalid_1's l1: 0.0129033\n",
      "[850]\ttraining's l1: 0.0127154\tvalid_1's l1: 0.012901\n",
      "[900]\ttraining's l1: 0.0127028\tvalid_1's l1: 0.0129008\n",
      "Early stopping, best iteration is:\n",
      "[874]\ttraining's l1: 0.0127086\tvalid_1's l1: 0.0128936\n",
      "fold  2\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.018165\tvalid_1's l1: 0.0179513\n",
      "[100]\ttraining's l1: 0.0151554\tvalid_1's l1: 0.0150017\n",
      "[150]\ttraining's l1: 0.0141155\tvalid_1's l1: 0.0139629\n",
      "[200]\ttraining's l1: 0.013632\tvalid_1's l1: 0.0134548\n",
      "[250]\ttraining's l1: 0.0133454\tvalid_1's l1: 0.0131843\n",
      "[300]\ttraining's l1: 0.0132029\tvalid_1's l1: 0.0130732\n",
      "[350]\ttraining's l1: 0.0131069\tvalid_1's l1: 0.0129959\n",
      "[400]\ttraining's l1: 0.013037\tvalid_1's l1: 0.012941\n",
      "[450]\ttraining's l1: 0.0129825\tvalid_1's l1: 0.0128886\n",
      "[500]\ttraining's l1: 0.0129352\tvalid_1's l1: 0.0128445\n",
      "[550]\ttraining's l1: 0.0128933\tvalid_1's l1: 0.012808\n",
      "[600]\ttraining's l1: 0.0128607\tvalid_1's l1: 0.0127839\n",
      "[650]\ttraining's l1: 0.0128311\tvalid_1's l1: 0.0127648\n",
      "[700]\ttraining's l1: 0.0128119\tvalid_1's l1: 0.0127455\n",
      "[750]\ttraining's l1: 0.0127881\tvalid_1's l1: 0.0127254\n",
      "[800]\ttraining's l1: 0.012765\tvalid_1's l1: 0.0127152\n",
      "[850]\ttraining's l1: 0.0127519\tvalid_1's l1: 0.0127129\n",
      "[900]\ttraining's l1: 0.0127372\tvalid_1's l1: 0.0127012\n",
      "[950]\ttraining's l1: 0.0127243\tvalid_1's l1: 0.0127003\n",
      "Early stopping, best iteration is:\n",
      "[907]\ttraining's l1: 0.0127339\tvalid_1's l1: 0.0126973\n",
      "fold  3\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0186237\tvalid_1's l1: 0.0191869\n",
      "[100]\ttraining's l1: 0.0151857\tvalid_1's l1: 0.0154938\n",
      "[150]\ttraining's l1: 0.0140427\tvalid_1's l1: 0.0143644\n",
      "[200]\ttraining's l1: 0.0135453\tvalid_1's l1: 0.0138899\n",
      "[250]\ttraining's l1: 0.013309\tvalid_1's l1: 0.0136574\n",
      "[300]\ttraining's l1: 0.0131405\tvalid_1's l1: 0.0135044\n",
      "[350]\ttraining's l1: 0.0130238\tvalid_1's l1: 0.0133973\n",
      "[400]\ttraining's l1: 0.0129402\tvalid_1's l1: 0.0133259\n",
      "[450]\ttraining's l1: 0.0128754\tvalid_1's l1: 0.013264\n",
      "[500]\ttraining's l1: 0.0128357\tvalid_1's l1: 0.013233\n",
      "[550]\ttraining's l1: 0.0127971\tvalid_1's l1: 0.013205\n",
      "[600]\ttraining's l1: 0.012765\tvalid_1's l1: 0.0131755\n",
      "[650]\ttraining's l1: 0.0127393\tvalid_1's l1: 0.0131614\n",
      "[700]\ttraining's l1: 0.0127151\tvalid_1's l1: 0.01314\n",
      "[750]\ttraining's l1: 0.0126963\tvalid_1's l1: 0.013129\n",
      "[800]\ttraining's l1: 0.0126759\tvalid_1's l1: 0.013102\n",
      "[850]\ttraining's l1: 0.0126584\tvalid_1's l1: 0.0130853\n",
      "[900]\ttraining's l1: 0.012646\tvalid_1's l1: 0.0130768\n",
      "[950]\ttraining's l1: 0.0126343\tvalid_1's l1: 0.0130746\n",
      "[1000]\ttraining's l1: 0.0126203\tvalid_1's l1: 0.0130609\n",
      "[1050]\ttraining's l1: 0.0126083\tvalid_1's l1: 0.0130536\n",
      "[1100]\ttraining's l1: 0.0125982\tvalid_1's l1: 0.0130498\n",
      "[1150]\ttraining's l1: 0.0125877\tvalid_1's l1: 0.0130441\n",
      "[1200]\ttraining's l1: 0.0125812\tvalid_1's l1: 0.0130485\n",
      "Early stopping, best iteration is:\n",
      "[1178]\ttraining's l1: 0.0125837\tvalid_1's l1: 0.0130414\n",
      "fold  4\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0181237\tvalid_1's l1: 0.0182231\n",
      "[100]\ttraining's l1: 0.01495\tvalid_1's l1: 0.0151518\n",
      "[150]\ttraining's l1: 0.0139994\tvalid_1's l1: 0.0143156\n",
      "[200]\ttraining's l1: 0.0135356\tvalid_1's l1: 0.0139133\n",
      "[250]\ttraining's l1: 0.0132697\tvalid_1's l1: 0.0136793\n",
      "[300]\ttraining's l1: 0.0131082\tvalid_1's l1: 0.0135411\n",
      "[350]\ttraining's l1: 0.0129798\tvalid_1's l1: 0.0134248\n",
      "[400]\ttraining's l1: 0.0128852\tvalid_1's l1: 0.0133514\n",
      "[450]\ttraining's l1: 0.0128237\tvalid_1's l1: 0.0133004\n",
      "[500]\ttraining's l1: 0.0127783\tvalid_1's l1: 0.0132723\n",
      "[550]\ttraining's l1: 0.0127412\tvalid_1's l1: 0.0132506\n",
      "[600]\ttraining's l1: 0.0127116\tvalid_1's l1: 0.0132338\n",
      "[650]\ttraining's l1: 0.0126874\tvalid_1's l1: 0.0132196\n",
      "[700]\ttraining's l1: 0.0126674\tvalid_1's l1: 0.0132199\n",
      "[750]\ttraining's l1: 0.0126497\tvalid_1's l1: 0.0132126\n",
      "[800]\ttraining's l1: 0.0126306\tvalid_1's l1: 0.0132064\n",
      "[850]\ttraining's l1: 0.012612\tvalid_1's l1: 0.0132\n",
      "[900]\ttraining's l1: 0.0125967\tvalid_1's l1: 0.0131909\n",
      "[950]\ttraining's l1: 0.012585\tvalid_1's l1: 0.0131885\n",
      "[1000]\ttraining's l1: 0.0125744\tvalid_1's l1: 0.0131853\n",
      "[1050]\ttraining's l1: 0.0125636\tvalid_1's l1: 0.01318\n",
      "[1100]\ttraining's l1: 0.0125544\tvalid_1's l1: 0.0131757\n",
      "[1150]\ttraining's l1: 0.0125436\tvalid_1's l1: 0.0131653\n",
      "[1200]\ttraining's l1: 0.0125345\tvalid_1's l1: 0.0131558\n",
      "[1250]\ttraining's l1: 0.0125276\tvalid_1's l1: 0.0131563\n",
      "Early stopping, best iteration is:\n",
      "[1210]\ttraining's l1: 0.0125321\tvalid_1's l1: 0.013152\n",
      "fold  5\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0184901\tvalid_1's l1: 0.0189972\n",
      "[100]\ttraining's l1: 0.0149927\tvalid_1's l1: 0.0155735\n",
      "[150]\ttraining's l1: 0.0139408\tvalid_1's l1: 0.0145616\n",
      "[200]\ttraining's l1: 0.0134523\tvalid_1's l1: 0.0141113\n",
      "[250]\ttraining's l1: 0.013186\tvalid_1's l1: 0.0138786\n",
      "[300]\ttraining's l1: 0.01304\tvalid_1's l1: 0.0137492\n",
      "[350]\ttraining's l1: 0.0129345\tvalid_1's l1: 0.0136687\n",
      "[400]\ttraining's l1: 0.0128458\tvalid_1's l1: 0.0135884\n",
      "[450]\ttraining's l1: 0.0127763\tvalid_1's l1: 0.0135364\n",
      "[500]\ttraining's l1: 0.0127346\tvalid_1's l1: 0.0135054\n",
      "[550]\ttraining's l1: 0.0126902\tvalid_1's l1: 0.013481\n",
      "[600]\ttraining's l1: 0.0126524\tvalid_1's l1: 0.0134663\n",
      "[650]\ttraining's l1: 0.0126278\tvalid_1's l1: 0.0134571\n",
      "[700]\ttraining's l1: 0.0126086\tvalid_1's l1: 0.0134403\n",
      "[750]\ttraining's l1: 0.0125929\tvalid_1's l1: 0.0134343\n",
      "[800]\ttraining's l1: 0.0125722\tvalid_1's l1: 0.0134196\n",
      "[850]\ttraining's l1: 0.0125569\tvalid_1's l1: 0.0134139\n",
      "[900]\ttraining's l1: 0.0125415\tvalid_1's l1: 0.0134056\n",
      "[950]\ttraining's l1: 0.0125293\tvalid_1's l1: 0.0133957\n",
      "[1000]\ttraining's l1: 0.0125195\tvalid_1's l1: 0.0133885\n",
      "[1050]\ttraining's l1: 0.0125094\tvalid_1's l1: 0.0133816\n",
      "[1100]\ttraining's l1: 0.0125001\tvalid_1's l1: 0.0133792\n",
      "Early stopping, best iteration is:\n",
      "[1080]\ttraining's l1: 0.0125038\tvalid_1's l1: 0.0133774\n",
      "------------------------------------------------------------------------------------------------------------------------\n",
      "mse  0.02091\n",
      "fold  1\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0184991\tvalid_1's l1: 0.0186401\n",
      "[100]\ttraining's l1: 0.015104\tvalid_1's l1: 0.0152883\n",
      "[150]\ttraining's l1: 0.0141446\tvalid_1's l1: 0.0143744\n",
      "[200]\ttraining's l1: 0.013677\tvalid_1's l1: 0.013938\n",
      "[250]\ttraining's l1: 0.0133774\tvalid_1's l1: 0.0136736\n",
      "[300]\ttraining's l1: 0.0131984\tvalid_1's l1: 0.0135318\n",
      "[350]\ttraining's l1: 0.0130768\tvalid_1's l1: 0.0134039\n",
      "[400]\ttraining's l1: 0.0129759\tvalid_1's l1: 0.0133174\n",
      "[450]\ttraining's l1: 0.0129144\tvalid_1's l1: 0.0132768\n",
      "[500]\ttraining's l1: 0.0128626\tvalid_1's l1: 0.0132344\n",
      "[550]\ttraining's l1: 0.0128092\tvalid_1's l1: 0.0132065\n",
      "[600]\ttraining's l1: 0.0127704\tvalid_1's l1: 0.0131708\n",
      "[650]\ttraining's l1: 0.0127389\tvalid_1's l1: 0.0131529\n",
      "[700]\ttraining's l1: 0.0127158\tvalid_1's l1: 0.0131364\n",
      "[750]\ttraining's l1: 0.0126969\tvalid_1's l1: 0.0131266\n",
      "[800]\ttraining's l1: 0.0126773\tvalid_1's l1: 0.0131154\n",
      "[850]\ttraining's l1: 0.0126613\tvalid_1's l1: 0.0131095\n",
      "[900]\ttraining's l1: 0.0126483\tvalid_1's l1: 0.0130995\n",
      "[950]\ttraining's l1: 0.0126363\tvalid_1's l1: 0.0130945\n",
      "[1000]\ttraining's l1: 0.0126244\tvalid_1's l1: 0.0130861\n",
      "[1050]\ttraining's l1: 0.0126163\tvalid_1's l1: 0.0130833\n",
      "Early stopping, best iteration is:\n",
      "[1012]\ttraining's l1: 0.012622\tvalid_1's l1: 0.0130827\n",
      "fold  2\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0186811\tvalid_1's l1: 0.0183618\n",
      "[100]\ttraining's l1: 0.0151113\tvalid_1's l1: 0.015028\n",
      "[150]\ttraining's l1: 0.0141121\tvalid_1's l1: 0.0141036\n",
      "[200]\ttraining's l1: 0.0136771\tvalid_1's l1: 0.0137274\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[250]\ttraining's l1: 0.0133886\tvalid_1's l1: 0.0134739\n",
      "[300]\ttraining's l1: 0.0132009\tvalid_1's l1: 0.0133139\n",
      "[350]\ttraining's l1: 0.0130728\tvalid_1's l1: 0.013208\n",
      "[400]\ttraining's l1: 0.0129879\tvalid_1's l1: 0.0131528\n",
      "[450]\ttraining's l1: 0.012923\tvalid_1's l1: 0.0130947\n",
      "[500]\ttraining's l1: 0.012873\tvalid_1's l1: 0.0130499\n",
      "[550]\ttraining's l1: 0.0128303\tvalid_1's l1: 0.0130127\n",
      "[600]\ttraining's l1: 0.0127978\tvalid_1's l1: 0.0129891\n",
      "[650]\ttraining's l1: 0.0127678\tvalid_1's l1: 0.0129739\n",
      "[700]\ttraining's l1: 0.0127452\tvalid_1's l1: 0.0129519\n",
      "[750]\ttraining's l1: 0.0127263\tvalid_1's l1: 0.0129389\n",
      "[800]\ttraining's l1: 0.0127053\tvalid_1's l1: 0.012923\n",
      "[850]\ttraining's l1: 0.0126926\tvalid_1's l1: 0.0129194\n",
      "[900]\ttraining's l1: 0.012682\tvalid_1's l1: 0.0129158\n",
      "[950]\ttraining's l1: 0.0126739\tvalid_1's l1: 0.0129158\n",
      "Early stopping, best iteration is:\n",
      "[912]\ttraining's l1: 0.0126797\tvalid_1's l1: 0.0129123\n",
      "fold  3\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.018246\tvalid_1's l1: 0.0181894\n",
      "[100]\ttraining's l1: 0.0150795\tvalid_1's l1: 0.0149247\n",
      "[150]\ttraining's l1: 0.0141358\tvalid_1's l1: 0.0139709\n",
      "[200]\ttraining's l1: 0.0136823\tvalid_1's l1: 0.0135329\n",
      "[250]\ttraining's l1: 0.0133713\tvalid_1's l1: 0.0132027\n",
      "[300]\ttraining's l1: 0.013204\tvalid_1's l1: 0.0130669\n",
      "[350]\ttraining's l1: 0.0130897\tvalid_1's l1: 0.012972\n",
      "[400]\ttraining's l1: 0.013013\tvalid_1's l1: 0.0129216\n",
      "[450]\ttraining's l1: 0.0129537\tvalid_1's l1: 0.0128692\n",
      "[500]\ttraining's l1: 0.0129085\tvalid_1's l1: 0.012829\n",
      "[550]\ttraining's l1: 0.0128612\tvalid_1's l1: 0.0127978\n",
      "[600]\ttraining's l1: 0.0128304\tvalid_1's l1: 0.012782\n",
      "[650]\ttraining's l1: 0.0128061\tvalid_1's l1: 0.0127724\n",
      "[700]\ttraining's l1: 0.0127804\tvalid_1's l1: 0.0127515\n",
      "[750]\ttraining's l1: 0.0127608\tvalid_1's l1: 0.0127444\n",
      "[800]\ttraining's l1: 0.0127473\tvalid_1's l1: 0.0127325\n",
      "[850]\ttraining's l1: 0.0127357\tvalid_1's l1: 0.0127273\n",
      "[900]\ttraining's l1: 0.0127194\tvalid_1's l1: 0.012725\n",
      "[950]\ttraining's l1: 0.0127076\tvalid_1's l1: 0.0127206\n",
      "[1000]\ttraining's l1: 0.0126951\tvalid_1's l1: 0.0127111\n",
      "[1050]\ttraining's l1: 0.0126864\tvalid_1's l1: 0.0127076\n",
      "Early stopping, best iteration is:\n",
      "[1026]\ttraining's l1: 0.01269\tvalid_1's l1: 0.0127031\n",
      "fold  4\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0183983\tvalid_1's l1: 0.0190424\n",
      "[100]\ttraining's l1: 0.0149434\tvalid_1's l1: 0.0156548\n",
      "[150]\ttraining's l1: 0.01396\tvalid_1's l1: 0.0147624\n",
      "[200]\ttraining's l1: 0.0134403\tvalid_1's l1: 0.0143031\n",
      "[250]\ttraining's l1: 0.0131761\tvalid_1's l1: 0.0140927\n",
      "[300]\ttraining's l1: 0.013026\tvalid_1's l1: 0.0139685\n",
      "[350]\ttraining's l1: 0.0128874\tvalid_1's l1: 0.0138517\n",
      "[400]\ttraining's l1: 0.0128017\tvalid_1's l1: 0.0137887\n",
      "[450]\ttraining's l1: 0.0127402\tvalid_1's l1: 0.0137406\n",
      "[500]\ttraining's l1: 0.0126848\tvalid_1's l1: 0.013703\n",
      "[550]\ttraining's l1: 0.0126448\tvalid_1's l1: 0.0136655\n",
      "[600]\ttraining's l1: 0.012612\tvalid_1's l1: 0.0136461\n",
      "[650]\ttraining's l1: 0.0125855\tvalid_1's l1: 0.0136296\n",
      "[700]\ttraining's l1: 0.0125627\tvalid_1's l1: 0.013616\n",
      "[750]\ttraining's l1: 0.0125387\tvalid_1's l1: 0.0136072\n",
      "[800]\ttraining's l1: 0.0125189\tvalid_1's l1: 0.0135963\n",
      "[850]\ttraining's l1: 0.0125048\tvalid_1's l1: 0.0135919\n",
      "[900]\ttraining's l1: 0.0124905\tvalid_1's l1: 0.0135841\n",
      "[950]\ttraining's l1: 0.0124776\tvalid_1's l1: 0.0135785\n",
      "[1000]\ttraining's l1: 0.0124678\tvalid_1's l1: 0.0135706\n",
      "[1050]\ttraining's l1: 0.0124573\tvalid_1's l1: 0.0135653\n",
      "[1100]\ttraining's l1: 0.0124478\tvalid_1's l1: 0.0135631\n",
      "[1150]\ttraining's l1: 0.0124401\tvalid_1's l1: 0.0135543\n",
      "[1200]\ttraining's l1: 0.0124316\tvalid_1's l1: 0.0135617\n",
      "Early stopping, best iteration is:\n",
      "[1166]\ttraining's l1: 0.0124376\tvalid_1's l1: 0.0135527\n",
      "fold  5\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0181111\tvalid_1's l1: 0.0185973\n",
      "[100]\ttraining's l1: 0.0151038\tvalid_1's l1: 0.0154105\n",
      "[150]\ttraining's l1: 0.0140576\tvalid_1's l1: 0.0143334\n",
      "[200]\ttraining's l1: 0.0136013\tvalid_1's l1: 0.0139082\n",
      "[250]\ttraining's l1: 0.0133386\tvalid_1's l1: 0.013689\n",
      "[300]\ttraining's l1: 0.0131654\tvalid_1's l1: 0.0135375\n",
      "[350]\ttraining's l1: 0.0130597\tvalid_1's l1: 0.013424\n",
      "[400]\ttraining's l1: 0.0129808\tvalid_1's l1: 0.0133324\n",
      "[450]\ttraining's l1: 0.0129209\tvalid_1's l1: 0.0132725\n",
      "[500]\ttraining's l1: 0.0128746\tvalid_1's l1: 0.0132106\n",
      "[550]\ttraining's l1: 0.012837\tvalid_1's l1: 0.0131781\n",
      "[600]\ttraining's l1: 0.0128004\tvalid_1's l1: 0.0131342\n",
      "[650]\ttraining's l1: 0.0127738\tvalid_1's l1: 0.0131134\n",
      "[700]\ttraining's l1: 0.0127484\tvalid_1's l1: 0.0130852\n",
      "[750]\ttraining's l1: 0.0127301\tvalid_1's l1: 0.0130507\n",
      "[800]\ttraining's l1: 0.0127143\tvalid_1's l1: 0.0130368\n",
      "[850]\ttraining's l1: 0.0126993\tvalid_1's l1: 0.0130279\n",
      "[900]\ttraining's l1: 0.0126838\tvalid_1's l1: 0.0130136\n",
      "[950]\ttraining's l1: 0.0126711\tvalid_1's l1: 0.0130012\n",
      "[1000]\ttraining's l1: 0.0126598\tvalid_1's l1: 0.0129942\n",
      "[1050]\ttraining's l1: 0.0126525\tvalid_1's l1: 0.0129846\n",
      "[1100]\ttraining's l1: 0.0126453\tvalid_1's l1: 0.0129762\n",
      "[1150]\ttraining's l1: 0.0126352\tvalid_1's l1: 0.0129631\n",
      "[1200]\ttraining's l1: 0.012625\tvalid_1's l1: 0.0129555\n",
      "[1250]\ttraining's l1: 0.0126125\tvalid_1's l1: 0.0129466\n",
      "[1300]\ttraining's l1: 0.012608\tvalid_1's l1: 0.0129401\n",
      "[1350]\ttraining's l1: 0.0126008\tvalid_1's l1: 0.0129361\n",
      "[1400]\ttraining's l1: 0.0125947\tvalid_1's l1: 0.0129274\n",
      "[1450]\ttraining's l1: 0.012591\tvalid_1's l1: 0.0129254\n",
      "[1500]\ttraining's l1: 0.0125865\tvalid_1's l1: 0.0129173\n",
      "[1550]\ttraining's l1: 0.0125811\tvalid_1's l1: 0.0129192\n",
      "Early stopping, best iteration is:\n",
      "[1519]\ttraining's l1: 0.012584\tvalid_1's l1: 0.0129155\n",
      "------------------------------------------------------------------------------------------------------------------------\n",
      "mse  0.0008\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/sunzhongyu/opt/anaconda3/envs/python36/lib/python3.6/site-packages/ipykernel_launcher.py:53: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n"
     ]
    }
   ],
   "source": [
    "train_x = train3[used_feat]\n",
    "train_y = train3['pressure']\n",
    "test_x = test3[used_feat]\n",
    "print(train_x.shape, test_x.shape)\n",
    "\n",
    "# -----------------------------------------------\n",
    "scores = []\n",
    "\n",
    "params = {'learning_rate': 0.1, \n",
    "        'boosting_type': 'gbdt', \n",
    "        'objective': 'regression_l1',\n",
    "        'metric': 'mae',\n",
    "        'min_child_samples': 46, \n",
    "        'min_child_weight': 0.01,\n",
    "        'feature_fraction': 0.8, \n",
    "        'bagging_fraction': 0.8, \n",
    "        'bagging_freq': 2, \n",
    "        'num_leaves': 16, \n",
    "        'max_depth': 5, \n",
    "        'n_jobs': -1, \n",
    "        'seed': 2019, \n",
    "        'verbosity': -1, \n",
    "       }\n",
    "\n",
    "\n",
    "\n",
    "oof_train = np.zeros(len(train_x))\n",
    "preds = np.zeros(len(test_x))\n",
    "folds = 5\n",
    "seeds = [2048, 1997] \n",
    "for seed in seeds: \n",
    "    kfold = KFold(n_splits=folds, shuffle=True, random_state=seed)\n",
    "    for fold, (trn_idx, val_idx) in enumerate(kfold.split(train_x, train_y)):\n",
    "        print('fold ', fold + 1)\n",
    "        x_trn, y_trn, x_val, y_val = train_x.iloc[trn_idx], train_y.iloc[trn_idx], train_x.iloc[val_idx], train_y.iloc[val_idx]\n",
    "        train_set = lgb.Dataset(x_trn, y_trn)\n",
    "        val_set = lgb.Dataset(x_val, y_val)\n",
    "\n",
    "        model = lgb.train(params, train_set, num_boost_round=500000,\n",
    "                          valid_sets=(train_set, val_set), early_stopping_rounds=50,\n",
    "                          verbose_eval=50)\n",
    "        oof_train[val_idx] += model.predict(x_val) / len(seeds)\n",
    "        preds += model.predict(test_x) / folds / len(seeds)\n",
    "        del x_trn, y_trn, x_val, y_val, model, train_set, val_set\n",
    "        gc.collect()\n",
    "    \n",
    "    mse = (mean_squared_error(oof_train, train3['pressure']))\n",
    "    \n",
    "    print('-'*120)\n",
    "    print('mse ', round(mse, 5))\n",
    "\n",
    "\n",
    "test3['pressure'] = preds + Mon_6_5_2019\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "分段4 训练集、测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "feat nums  3 ['MeasName', 'Hour', 'Day']\n"
     ]
    }
   ],
   "source": [
    "train2019Mon9 = train2019[(train2019['Time_time'] >= '2019-9-1') & (train2019['Time_time'] <= '2019-9-30')]\n",
    "train2019Mon8 = train2019[(train2019['Time_time'] >= '2019-8-1') & (train2019['Time_time'] <= '2019-8-30')]\n",
    "Mon_9_8_2019 = train2019Mon9['pressure'].mean() - train2019Mon8['pressure'].mean()\n",
    "\n",
    "\n",
    "train4 = train2020[(train2020['Time_time'] >= '2020-8-1') & (train2020['Time_time'] <= '2020-8-31')]\n",
    "test4 = test[(test['Time_time'] >= '2020-9-7') & (test['Time_time'] <= '2020-9-20')]\n",
    "\n",
    "used_feat = [f for f in train4.columns if f not in ['id', 'pressure', 'Time', 'Time_time']]\n",
    "print('feat nums ', len(used_feat), used_feat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(22320, 3) (10080, 3)\n",
      "fold  1\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0153139\tvalid_1's l1: 0.0152649\n",
      "[100]\ttraining's l1: 0.0128988\tvalid_1's l1: 0.0130196\n",
      "[150]\ttraining's l1: 0.0122528\tvalid_1's l1: 0.0124379\n",
      "[200]\ttraining's l1: 0.0119947\tvalid_1's l1: 0.0122026\n",
      "[250]\ttraining's l1: 0.0118477\tvalid_1's l1: 0.012072\n",
      "[300]\ttraining's l1: 0.0117407\tvalid_1's l1: 0.0119733\n",
      "[350]\ttraining's l1: 0.0116779\tvalid_1's l1: 0.0119222\n",
      "[400]\ttraining's l1: 0.0116208\tvalid_1's l1: 0.01187\n",
      "[450]\ttraining's l1: 0.0115897\tvalid_1's l1: 0.0118553\n",
      "[500]\ttraining's l1: 0.0115605\tvalid_1's l1: 0.0118433\n",
      "[550]\ttraining's l1: 0.011537\tvalid_1's l1: 0.0118216\n",
      "[600]\ttraining's l1: 0.0115184\tvalid_1's l1: 0.0118109\n",
      "[650]\ttraining's l1: 0.0115015\tvalid_1's l1: 0.011808\n",
      "[700]\ttraining's l1: 0.0114864\tvalid_1's l1: 0.0117956\n",
      "[750]\ttraining's l1: 0.0114753\tvalid_1's l1: 0.0117893\n",
      "[800]\ttraining's l1: 0.0114626\tvalid_1's l1: 0.0117855\n",
      "[850]\ttraining's l1: 0.0114533\tvalid_1's l1: 0.0117787\n",
      "[900]\ttraining's l1: 0.0114426\tvalid_1's l1: 0.0117758\n",
      "[950]\ttraining's l1: 0.0114345\tvalid_1's l1: 0.0117715\n",
      "[1000]\ttraining's l1: 0.0114242\tvalid_1's l1: 0.0117604\n",
      "[1050]\ttraining's l1: 0.0114163\tvalid_1's l1: 0.0117557\n",
      "[1100]\ttraining's l1: 0.0114077\tvalid_1's l1: 0.0117567\n",
      "[1150]\ttraining's l1: 0.0114018\tvalid_1's l1: 0.0117524\n",
      "Early stopping, best iteration is:\n",
      "[1136]\ttraining's l1: 0.0114035\tvalid_1's l1: 0.0117504\n",
      "fold  2\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0161075\tvalid_1's l1: 0.01637\n",
      "[100]\ttraining's l1: 0.0130153\tvalid_1's l1: 0.0130118\n",
      "[150]\ttraining's l1: 0.0122894\tvalid_1's l1: 0.0122633\n",
      "[200]\ttraining's l1: 0.0120052\tvalid_1's l1: 0.0119885\n",
      "[250]\ttraining's l1: 0.0118554\tvalid_1's l1: 0.0118656\n",
      "[300]\ttraining's l1: 0.0117391\tvalid_1's l1: 0.0117641\n",
      "[350]\ttraining's l1: 0.0116721\tvalid_1's l1: 0.011729\n",
      "[400]\ttraining's l1: 0.0116171\tvalid_1's l1: 0.011688\n",
      "[450]\ttraining's l1: 0.0115805\tvalid_1's l1: 0.0116714\n",
      "[500]\ttraining's l1: 0.0115542\tvalid_1's l1: 0.0116542\n",
      "Early stopping, best iteration is:\n",
      "[498]\ttraining's l1: 0.0115547\tvalid_1's l1: 0.0116527\n",
      "fold  3\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0155656\tvalid_1's l1: 0.0157754\n",
      "[100]\ttraining's l1: 0.0130142\tvalid_1's l1: 0.0131538\n",
      "[150]\ttraining's l1: 0.012317\tvalid_1's l1: 0.0124664\n",
      "[200]\ttraining's l1: 0.0120476\tvalid_1's l1: 0.0122533\n",
      "[250]\ttraining's l1: 0.0118746\tvalid_1's l1: 0.0121043\n",
      "[300]\ttraining's l1: 0.0117578\tvalid_1's l1: 0.0120123\n",
      "[350]\ttraining's l1: 0.011685\tvalid_1's l1: 0.011948\n",
      "[400]\ttraining's l1: 0.0116156\tvalid_1's l1: 0.0118836\n",
      "[450]\ttraining's l1: 0.0115727\tvalid_1's l1: 0.0118704\n",
      "[500]\ttraining's l1: 0.0115372\tvalid_1's l1: 0.0118479\n",
      "[550]\ttraining's l1: 0.0115112\tvalid_1's l1: 0.0118319\n",
      "[600]\ttraining's l1: 0.01149\tvalid_1's l1: 0.0118151\n",
      "[650]\ttraining's l1: 0.0114718\tvalid_1's l1: 0.0118089\n",
      "[700]\ttraining's l1: 0.0114566\tvalid_1's l1: 0.011805\n",
      "[750]\ttraining's l1: 0.0114427\tvalid_1's l1: 0.0117997\n",
      "[800]\ttraining's l1: 0.0114333\tvalid_1's l1: 0.0117919\n",
      "[850]\ttraining's l1: 0.0114232\tvalid_1's l1: 0.0117827\n",
      "[900]\ttraining's l1: 0.0114138\tvalid_1's l1: 0.0117774\n",
      "[950]\ttraining's l1: 0.0114038\tvalid_1's l1: 0.0117788\n",
      "[1000]\ttraining's l1: 0.0113951\tvalid_1's l1: 0.011774\n",
      "Early stopping, best iteration is:\n",
      "[999]\ttraining's l1: 0.0113952\tvalid_1's l1: 0.0117733\n",
      "fold  4\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0158459\tvalid_1's l1: 0.0156636\n",
      "[100]\ttraining's l1: 0.0129235\tvalid_1's l1: 0.0128413\n",
      "[150]\ttraining's l1: 0.0123103\tvalid_1's l1: 0.0123054\n",
      "[200]\ttraining's l1: 0.0120578\tvalid_1's l1: 0.0120893\n",
      "[250]\ttraining's l1: 0.0118664\tvalid_1's l1: 0.0119446\n",
      "[300]\ttraining's l1: 0.011774\tvalid_1's l1: 0.0118898\n",
      "[350]\ttraining's l1: 0.0117012\tvalid_1's l1: 0.0118492\n",
      "[400]\ttraining's l1: 0.0116422\tvalid_1's l1: 0.011834\n",
      "[450]\ttraining's l1: 0.0116046\tvalid_1's l1: 0.0118088\n",
      "[500]\ttraining's l1: 0.0115724\tvalid_1's l1: 0.0117974\n",
      "[550]\ttraining's l1: 0.0115448\tvalid_1's l1: 0.0117785\n",
      "[600]\ttraining's l1: 0.0115193\tvalid_1's l1: 0.0117719\n",
      "[650]\ttraining's l1: 0.0114984\tvalid_1's l1: 0.0117668\n",
      "Early stopping, best iteration is:\n",
      "[641]\ttraining's l1: 0.0115003\tvalid_1's l1: 0.0117645\n",
      "fold  5\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0156492\tvalid_1's l1: 0.015905\n",
      "[100]\ttraining's l1: 0.0129471\tvalid_1's l1: 0.0135257\n",
      "[150]\ttraining's l1: 0.0122251\tvalid_1's l1: 0.0128726\n",
      "[200]\ttraining's l1: 0.0119376\tvalid_1's l1: 0.0126131\n",
      "[250]\ttraining's l1: 0.0117729\tvalid_1's l1: 0.0124718\n",
      "[300]\ttraining's l1: 0.0116712\tvalid_1's l1: 0.0123735\n",
      "[350]\ttraining's l1: 0.0115908\tvalid_1's l1: 0.0123029\n",
      "[400]\ttraining's l1: 0.0115385\tvalid_1's l1: 0.0122526\n",
      "[450]\ttraining's l1: 0.011501\tvalid_1's l1: 0.0122328\n",
      "[500]\ttraining's l1: 0.0114678\tvalid_1's l1: 0.0122016\n",
      "[550]\ttraining's l1: 0.0114408\tvalid_1's l1: 0.0121867\n",
      "[600]\ttraining's l1: 0.0114188\tvalid_1's l1: 0.0121668\n",
      "[650]\ttraining's l1: 0.0114012\tvalid_1's l1: 0.0121564\n",
      "[700]\ttraining's l1: 0.0113858\tvalid_1's l1: 0.0121535\n",
      "[750]\ttraining's l1: 0.0113701\tvalid_1's l1: 0.0121372\n",
      "[800]\ttraining's l1: 0.0113577\tvalid_1's l1: 0.0121251\n",
      "[850]\ttraining's l1: 0.0113483\tvalid_1's l1: 0.012119\n",
      "Early stopping, best iteration is:\n",
      "[840]\ttraining's l1: 0.0113499\tvalid_1's l1: 0.0121169\n",
      "------------------------------------------------------------------------------------------------------------------------\n",
      "mse  0.01888\n",
      "fold  1\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0161545\tvalid_1's l1: 0.0161458\n",
      "[100]\ttraining's l1: 0.0130846\tvalid_1's l1: 0.0130843\n",
      "[150]\ttraining's l1: 0.0123041\tvalid_1's l1: 0.0122692\n",
      "[200]\ttraining's l1: 0.012002\tvalid_1's l1: 0.0119907\n",
      "[250]\ttraining's l1: 0.0118328\tvalid_1's l1: 0.0118163\n",
      "[300]\ttraining's l1: 0.0117306\tvalid_1's l1: 0.0117202\n",
      "[350]\ttraining's l1: 0.0116596\tvalid_1's l1: 0.0116758\n",
      "[400]\ttraining's l1: 0.0116122\tvalid_1's l1: 0.0116263\n",
      "[450]\ttraining's l1: 0.0115855\tvalid_1's l1: 0.0116127\n",
      "[500]\ttraining's l1: 0.0115597\tvalid_1's l1: 0.0115945\n",
      "[550]\ttraining's l1: 0.011543\tvalid_1's l1: 0.0115867\n",
      "[600]\ttraining's l1: 0.011524\tvalid_1's l1: 0.0115814\n",
      "[650]\ttraining's l1: 0.0115088\tvalid_1's l1: 0.0115765\n",
      "Early stopping, best iteration is:\n",
      "[646]\ttraining's l1: 0.0115103\tvalid_1's l1: 0.0115742\n",
      "fold  2\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.016251\tvalid_1's l1: 0.0167842\n",
      "[100]\ttraining's l1: 0.0129254\tvalid_1's l1: 0.013397\n",
      "[150]\ttraining's l1: 0.0121826\tvalid_1's l1: 0.0126631\n",
      "[200]\ttraining's l1: 0.0119119\tvalid_1's l1: 0.0124206\n",
      "[250]\ttraining's l1: 0.0117623\tvalid_1's l1: 0.0122974\n",
      "[300]\ttraining's l1: 0.0116521\tvalid_1's l1: 0.0122061\n",
      "[350]\ttraining's l1: 0.0115825\tvalid_1's l1: 0.0121454\n",
      "[400]\ttraining's l1: 0.011538\tvalid_1's l1: 0.0121095\n",
      "[450]\ttraining's l1: 0.0114914\tvalid_1's l1: 0.0120716\n",
      "[500]\ttraining's l1: 0.0114692\tvalid_1's l1: 0.0120608\n",
      "[550]\ttraining's l1: 0.0114514\tvalid_1's l1: 0.0120496\n",
      "[600]\ttraining's l1: 0.011433\tvalid_1's l1: 0.0120365\n",
      "[650]\ttraining's l1: 0.0114136\tvalid_1's l1: 0.0120208\n",
      "[700]\ttraining's l1: 0.0114\tvalid_1's l1: 0.0120107\n",
      "[750]\ttraining's l1: 0.0113849\tvalid_1's l1: 0.0120027\n",
      "[800]\ttraining's l1: 0.0113745\tvalid_1's l1: 0.011997\n",
      "[850]\ttraining's l1: 0.0113648\tvalid_1's l1: 0.0119928\n",
      "[900]\ttraining's l1: 0.0113562\tvalid_1's l1: 0.0119923\n",
      "[950]\ttraining's l1: 0.0113496\tvalid_1's l1: 0.011988\n",
      "Early stopping, best iteration is:\n",
      "[942]\ttraining's l1: 0.0113506\tvalid_1's l1: 0.0119866\n",
      "fold  3\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0156551\tvalid_1's l1: 0.0156806\n",
      "[100]\ttraining's l1: 0.0128767\tvalid_1's l1: 0.013116\n",
      "[150]\ttraining's l1: 0.0122732\tvalid_1's l1: 0.0125017\n",
      "[200]\ttraining's l1: 0.0119783\tvalid_1's l1: 0.0122596\n",
      "[250]\ttraining's l1: 0.0118139\tvalid_1's l1: 0.012104\n",
      "[300]\ttraining's l1: 0.0117066\tvalid_1's l1: 0.0120099\n",
      "[350]\ttraining's l1: 0.0116277\tvalid_1's l1: 0.0119668\n",
      "[400]\ttraining's l1: 0.011583\tvalid_1's l1: 0.0119406\n",
      "[450]\ttraining's l1: 0.0115559\tvalid_1's l1: 0.011928\n",
      "[500]\ttraining's l1: 0.0115315\tvalid_1's l1: 0.0119055\n",
      "[550]\ttraining's l1: 0.0115084\tvalid_1's l1: 0.0118863\n",
      "[600]\ttraining's l1: 0.0114881\tvalid_1's l1: 0.011871\n",
      "[650]\ttraining's l1: 0.0114702\tvalid_1's l1: 0.0118626\n",
      "[700]\ttraining's l1: 0.0114559\tvalid_1's l1: 0.0118496\n",
      "[750]\ttraining's l1: 0.0114445\tvalid_1's l1: 0.0118402\n",
      "[800]\ttraining's l1: 0.0114337\tvalid_1's l1: 0.0118292\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[850]\ttraining's l1: 0.0114267\tvalid_1's l1: 0.0118271\n",
      "[900]\ttraining's l1: 0.0114207\tvalid_1's l1: 0.0118168\n",
      "[950]\ttraining's l1: 0.0114128\tvalid_1's l1: 0.0118222\n",
      "Early stopping, best iteration is:\n",
      "[919]\ttraining's l1: 0.0114177\tvalid_1's l1: 0.0118146\n",
      "fold  4\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0159187\tvalid_1's l1: 0.0158127\n",
      "[100]\ttraining's l1: 0.0130334\tvalid_1's l1: 0.012961\n",
      "[150]\ttraining's l1: 0.0123057\tvalid_1's l1: 0.0123045\n",
      "[200]\ttraining's l1: 0.0120389\tvalid_1's l1: 0.0120522\n",
      "[250]\ttraining's l1: 0.0118855\tvalid_1's l1: 0.0119122\n",
      "[300]\ttraining's l1: 0.0117798\tvalid_1's l1: 0.0118384\n",
      "[350]\ttraining's l1: 0.0117015\tvalid_1's l1: 0.011794\n",
      "[400]\ttraining's l1: 0.0116494\tvalid_1's l1: 0.0117739\n",
      "[450]\ttraining's l1: 0.0116096\tvalid_1's l1: 0.0117469\n",
      "[500]\ttraining's l1: 0.0115725\tvalid_1's l1: 0.011727\n",
      "[550]\ttraining's l1: 0.011547\tvalid_1's l1: 0.0117216\n",
      "[600]\ttraining's l1: 0.011524\tvalid_1's l1: 0.011709\n",
      "[650]\ttraining's l1: 0.0115018\tvalid_1's l1: 0.0117024\n",
      "[700]\ttraining's l1: 0.0114883\tvalid_1's l1: 0.0117003\n",
      "Early stopping, best iteration is:\n",
      "[671]\ttraining's l1: 0.0114968\tvalid_1's l1: 0.0116986\n",
      "fold  5\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[50]\ttraining's l1: 0.0152363\tvalid_1's l1: 0.0153756\n",
      "[100]\ttraining's l1: 0.0128766\tvalid_1's l1: 0.013\n",
      "[150]\ttraining's l1: 0.0122447\tvalid_1's l1: 0.0124172\n",
      "[200]\ttraining's l1: 0.0119579\tvalid_1's l1: 0.0122464\n",
      "[250]\ttraining's l1: 0.0117962\tvalid_1's l1: 0.0121313\n",
      "[300]\ttraining's l1: 0.0116832\tvalid_1's l1: 0.0120453\n",
      "[350]\ttraining's l1: 0.0116104\tvalid_1's l1: 0.0120126\n",
      "[400]\ttraining's l1: 0.0115521\tvalid_1's l1: 0.0119822\n",
      "[450]\ttraining's l1: 0.0115087\tvalid_1's l1: 0.0119645\n",
      "[500]\ttraining's l1: 0.0114838\tvalid_1's l1: 0.0119523\n",
      "[550]\ttraining's l1: 0.0114625\tvalid_1's l1: 0.0119541\n",
      "Early stopping, best iteration is:\n",
      "[524]\ttraining's l1: 0.0114737\tvalid_1's l1: 0.0119473\n",
      "------------------------------------------------------------------------------------------------------------------------\n",
      "mse  0.00052\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/sunzhongyu/opt/anaconda3/envs/python36/lib/python3.6/site-packages/ipykernel_launcher.py:52: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n"
     ]
    }
   ],
   "source": [
    "train_x = train4[used_feat]\n",
    "train_y = train4['pressure']\n",
    "test_x = test4[used_feat]\n",
    "print(train_x.shape, test_x.shape)\n",
    "\n",
    "# -----------------------------------------------\n",
    "scores = []\n",
    "\n",
    "params = {'learning_rate': 0.1, \n",
    "        'boosting_type': 'gbdt', \n",
    "        'objective': 'regression_l1',\n",
    "        'metric': 'mae',\n",
    "        'min_child_samples': 46, \n",
    "        'min_child_weight': 0.01,\n",
    "        'feature_fraction': 0.8, \n",
    "        'bagging_fraction': 0.8, \n",
    "        'bagging_freq': 2, \n",
    "        'num_leaves': 16, \n",
    "        'max_depth': 5, \n",
    "        'n_jobs': -1, \n",
    "        'seed': 2019, \n",
    "        'verbosity': -1, \n",
    "       }\n",
    "\n",
    "\n",
    "\n",
    "oof_train = np.zeros(len(train_x))\n",
    "preds = np.zeros(len(test_x))\n",
    "folds = 5\n",
    "seeds = [2048, 1997] \n",
    "for seed in seeds: \n",
    "    kfold = KFold(n_splits=folds, shuffle=True, random_state=seed)\n",
    "    for fold, (trn_idx, val_idx) in enumerate(kfold.split(train_x, train_y)):\n",
    "        print('fold ', fold + 1)\n",
    "        x_trn, y_trn, x_val, y_val = train_x.iloc[trn_idx], train_y.iloc[trn_idx], train_x.iloc[val_idx], train_y.iloc[val_idx]\n",
    "        train_set = lgb.Dataset(x_trn, y_trn)\n",
    "        val_set = lgb.Dataset(x_val, y_val)\n",
    "\n",
    "        model = lgb.train(params, train_set, num_boost_round=500000,\n",
    "                          valid_sets=(train_set, val_set), early_stopping_rounds=50,\n",
    "                          verbose_eval=50)\n",
    "        oof_train[val_idx] += model.predict(x_val) / len(seeds)\n",
    "        preds += model.predict(test_x) / folds / len(seeds)\n",
    "        del x_trn, y_trn, x_val, y_val, model, train_set, val_set\n",
    "        gc.collect()\n",
    "    \n",
    "    mse = (mean_squared_error(oof_train, train4['pressure']))\n",
    "    \n",
    "    print('-'*120)\n",
    "    print('mse ', round(mse, 5))\n",
    "    \n",
    "test4['pressure'] = preds + Mon_9_8_2019"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "test = pd.concat([test1, test2, test3, test4], axis = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>Time</th>\n",
       "      <th>MeasName</th>\n",
       "      <th>Hour</th>\n",
       "      <th>Time_time</th>\n",
       "      <th>Day</th>\n",
       "      <th>pressure</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>3</td>\n",
       "      <td>0.439729</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>3</td>\n",
       "      <td>0.452367</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>3</td>\n",
       "      <td>0.459496</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>3</td>\n",
       "      <td>0.464083</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>2020-02-03</td>\n",
       "      <td>3</td>\n",
       "      <td>0.466106</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id        Time  MeasName  Hour  Time_time  Day  pressure\n",
       "0   0  2020-02-03         4     0 2020-02-03    3  0.439729\n",
       "1   1  2020-02-03         4     1 2020-02-03    3  0.452367\n",
       "2   2  2020-02-03         4     2 2020-02-03    3  0.459496\n",
       "3   3  2020-02-03         4     3 2020-02-03    3  0.464083\n",
       "4   4  2020-02-03         4     4 2020-02-03    3  0.466106"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test[['id', 'pressure']].to_csv('../sub/submit.csv', index = False)\n",
    "test.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
